When I create an object of the class in ruby, the console returns a Hex code. What is the significance of this hex code?
apple = Fruits.new => #Fruits:0x00005569446a55d8
When I create an object of the class in ruby, the console returns a Hex code. What is the significance of this hex code?
apple = Fruits.new => #Fruits:0x00005569446a55d8
I'm assuming you're running this in irb, or some sort of interactive console. In this scenario inspect
is used to display a representation of apple
. If you haven't defined your own inspect
in the Fruits
class, then it's inherited from Object
.
inspect → string
Returns a string containing a human-readable representation of obj. The default
inspect
shows the object's class name, an encoding of its memory address, and a list of the instance variables and their values (by callinginspect
on each of them). User defined classes should override this method to provide a better representation of obj. When overriding this method, it should return a string whose encoding is compatible with the default external encoding.[ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]" Time.new.inspect #=> "2008-03-08 19:43:39 +0900" class Foo end Foo.new.inspect #=> "#<Foo:0x0300c868>" class Bar def initialize @bar = 1 end end Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"