0x0c: These shoes are made for walking

Wanna go for a stroll? It is getting winter again, at least in this part of the world so strolling outside might be a bit, err, cold. Instead I suggest you just walk some ruby objects this time instead?

Walking – in this context – means visiting an object and all connected subobjects, and visiting them only once. So to walk an object you would just get the object, collect all subobjects (read: instance variables), and any other connected object you know about: for Arrays these are the array elements, for hashes all keys and values. And then we use some piece of storage that remembers which object we already went to, to visit each one only once, and at the same time to prevent endless loops (which would otherwise be easy with objects that store “both ends of a link”, i.e.

What is this good for?

Again you can do a number of different things, some even remotely interesting :) Say you want to determine the ‘mtime’ (in Unix lingo this is the time of the last modification) of an object, and say you have mtime_base methods on all supporting classes, that return the base modification time for that object only: