I probably should take a course on using google, but I just can’t find out an answer.
When one calls to_s
on the instance of class, that does not have an ancestor, overriding to_s
, Object#to_s
is being called and the output looks like:
"#<TestClass:0x136e3544>"
According to the documentation:
The default
to_s
prints the object’s class and an encoding of the object id.
What the heck means “encoded” here? It’s not an id
itself:
▶ t = TestClass.new.to_s
#⇒ "#<TestClass:0x137e6b94>"
▶ t.__id__
#⇒ 163526070
▶ "0x%08x" % t.__id__
#⇒ "0x09bf35b6"
According to ruby source, it is a heap memory address:
str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
Now the question is: is there any relation between __id__
and what’s being printed in default to_s
at all?
NB Credits to @MarekLipka: possibly this behavior may be reproduced under VirtualBox only. It sounds like on native env the following does the trick:
"0x%08x" % (t.__id__ << 1)