Short version: The default inspect method for a class displays the address of an object. * How can I do this in my own inspect method?
* (To be clear, I want the 8-digit hexadecimal number that you usually get from inspect . I don't care about the actual memory address. I just call it the memory address because it looks like this: I know that Ruby is memory safe. )
Long version: I have two classes: Thing and ThingList . ThingList is a subclass of Array specifically designed to store Things. Due to the nature of Things and the way they are used in my program, Things have an @container instance variable that points to a ThingList that contains Thing .
It is possible that two things have exactly the same data. So when I debug an application, the only way I can reliably distinguish between two things is to use inspect , which displays their address. However, when I inspect a Thing , I get pages on the output pages, because inspect will recursively check @container , forcing it to check every Thing in the list as well!
All I need is the first part of this conclusion. How to write my own inspect method in Thing that will only display this?
#<Thing:0xb7727704>
EDIT: I just realized that to_s does just that. I have not noticed this before, because I have a custom to_s that provides human readable information about the object.
Suppose I cannot use to_s and that I have to write a custom inspect .
source share