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 [TODO: Code shorthand span ] 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:

ObjectSpace.each_object

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 application2 .

Here's the start of a simplistic example: A [TODO: Code shorthand span ] class that stores first and last names and a few instances that aren't assigned to anything.

# Sample part 1

class Character
  attr_reader :first_name, :last_name
  def initialize (name)
    @first_name, @last_name = name.split(/ /)
  end
end

names = [ "John Robinson", "Maureen Robinson", "Zachary Smith",
  "Judy Robinson", "Will Robinson", "Penny Robinson", "Don West"
]

names.each do |name|
  Character.new(name)   # Lost in Space?
end

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 [TODO: Code shorthand span ] the [TODO: Code shorthand span ] class name and examining the [TODO: Code shorthand span ] property of each object returned in the loop.

# Sample part 2

ObjectSpace.each_object(Character) do |obj|
  if obj.last_name != "Robinson"
    puts "#{obj.first_name} #{obj.last_name}"
  end
end

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, [TODO: Code shorthand span ] can provide a nice bridge.

Footnotes:

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.

~ fin ~