Is there a way to print out the memory location of a Ruby object, not from puts self as I want to have a custom to_s method and print the memory pointer location as one of the pieces of information to be output.
Is there a way to print out the memory location of a Ruby object,
Share
I don’t know why would you want such a feature, and this is very implementation-specific, but on MRI 1.9 you can (ab)use
object_id:For an explanation of why does it work, check out the relevant lines in MRI
gc.c.Note that this will not work on other implementations (JRuby, Rubinius), and may very well break in future versions of Ruby. Also, there may be no way at all to get the address in some implementations (I suspect that you cannot in JRuby; I’m not sure, through).