在 ruby 中创建 class 的对象时,控制台给出的十六进制代码是什么
What is the hex code console gives when creating object of a class in ruby
当我在ruby中创建class的对象时,控制台returns一个十六进制代码。这个十六进制代码有什么意义?
苹果=Fruits.new
=> #Fruits:0x00005569446a55d8
我假设您在 irb 或某种交互式控制台中 运行。在这种情况下 inspect
用于显示 apple
的表示。如果你没有在Fruits
class中定义你自己的inspect
,那么它继承自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 calling inspect
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>"
当我在ruby中创建class的对象时,控制台returns一个十六进制代码。这个十六进制代码有什么意义?
苹果=Fruits.new => #Fruits:0x00005569446a55d8
我假设您在 irb 或某种交互式控制台中 运行。在这种情况下 inspect
用于显示 apple
的表示。如果你没有在Fruits
class中定义你自己的inspect
,那么它继承自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>"