The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Finding Things in Rubys ObjectSpace

TODO: Pull subtitle into page object

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:

code_start_default_section code_end_default_section

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.

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.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Finding Things in Rubys ObjectSpace

-- p

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

-- p

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

-- p

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

-- p

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

-- code/
-- ruby{numberLines: true}

ObjectSpace.each_object

-- /code

-- p

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

-- p

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.

-- code/
-- Ruby{numberLines: true}

# 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

-- /code

-- p

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.

-- code/
-- ruby{numberLines: true}

# Sample part 2

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

-- /code

-- p

Running that, we get our list:

-- p

Don West
    Zachary Smith

-- p

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.

-- p

Footnotes:

-- p

1. <<link|Design Patterns in Ruby|http://designpatternsinruby.com>> "... 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.

-- p

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.


-- categories
-- Ruby

-- metadata
-- date: 2015-03-19 00:00:00
-- id: 20en2pnm
-- status: published
-- type: post
-- SCRUBBED_NEO: false
-- site: aws