Friday 10 February 2012

How to identify that object belong to which class?



irb(main):001:0> str = Class.new(String)
=> #<Class:0xb5cfc3c0>


irb(main):002:0> str.class
=> Class


irb(main):003:0> str.parent
=> Object


irb(main):004:0> str.class.superclass
=> Module


irb(main):005:0> str.superclass
=> String



my_str = str.new("hello")
=> "hello"


irb(main):008:0> my_str.class
=> #<Class:0xb5e66080>


irb(main):009:0> my_str.parent
NoMethodError: undefined method `parent' for "hello":#<Class:0xb5e66080>
from (irb):9


irb(main):010:0> my_str.class.superclass
=> String


irb(main):011:0> my_str.class.parent
=> Object

irb(main):012:0> my_str.class.superclass.parent
=> Object


No comments:

Post a Comment