Finding Things in Rubys ObjectSpace

Here's a great development nugget courtesy of the book _Design Patterns in Ruby_^1^^.

The standard Ruby library contains a module called `ObjectSpace` that:

< ... contains a number of routines that interact with the garbage collection facility and allow you to traverse all living objects with an iterator.

It's that last part that's of interest and leads us to the method:

Pass it a class name and it'll iterate over all existing objects of that type. That means we can find specific objects from anywhere in an application^2^^.

Here's the start of a simplistic example: A `Character` class that stores first and last names and a few instances that aren't assigned to anything.

To demonstrate the method, let's produce a list of everyone who's not a member of the Robinson family. This is done by passing `ObjectSpace.each_object` the `Character` class name and examining the `last_name` property of each object returned in the loop.

Running that, we get our list:

Don West Zachary Smith

There are usually explicit connections between objects that need to know about each other. When there's not and it would be tricky to implement one, `ObjectSpace.each_object` can provide a nice bridge.


1. Design Patterns in Ruby "... a guide to solving real-world problems with Ruby." I'm only seven chapters in. If I get nothing else out of it, it's already been worth the price of admission. This is one of those books that fits squarely into both the "Wish I'd read it years ago" and the "To re-read repeatedly" categories.

2. Update on January 5, 2017 - Important to point out this is probably a bad idea most of the time. I was just intrigued to see that it was possible.