SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    object to class...

    is it possible to convert from a ruby object to Ruby class...
    and, how to find the included classes in ruby program....
    any idea.....

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is it possible to convert from a ruby object to Ruby class...
    What does it mean to convert an object to a class?

    and, how to find the included classes in ruby program....
    Code ruby:
    ObjectSpace.each_object(Class) do |c|
      puts c
    end

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    What does it mean to convert an object to a class?
    Maybe identify the class of an object?

    Code Ruby:
    class Dog
    end
     
    d = Dog.new 
    puts d.class   #Dog

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried the code you've entered as being what you'd like to happen? I ask because I think it will do just that:
    Code:
    puts d.class
    This line will return "Dog".

    Have a look at this description of the methods of the class Object:

    http://whytheluckystiff.net/ruby/pic...l#Object.class

    All classes have Object as a parent and therefore will have the same methods.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried the code you've entered as being what you'd like to happen?
    Yes.

    I ask because I think it will do just that:
    What does 'it' refer to? What does 'that' refer to?

    This line will return "Dog".
    Hmm...maybe that's why I put "Dog" as a comment on that line.

    All classes have Object as a parent and therefore will have the same methods.
    My tests are unable to confirm that statement:

    Code Ruby:
    class Dog
      def bark
        puts "Bow wow."
      end
    end
     
    class Cat
    end
     
    d = Dog.new
    d.bark
     
    c = Cat
    c.bark
     
    --output:--
    Bow wow.
    r1test.rb:14: undefined method `bark' for Cat:Class (NoMethodError)
    Based on the error message, it does not appear that all classes have the same methods.

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think he means that all objects support the methods listed on the page he links to.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •