Q:

Using a hash as a factory in Ruby

I want to use a hash to instantiate objects of a certain type, however issues soon arise because the hash will only instantiate one instance, and then forever 'point' to that instance. 
class Person
end

factory = { "make_person" => Person.new }

a = factory["make_person"]
b = factory["make_person"]

a == b
=> true

Even if the hash points to a method, say for instance make_person, which calls inside of it Person.new, it will still forever point to a single instance.
Anybody have a workaround for this? Thanks much.

A:

The object (instance of Person) is being assigned to the "make_person" key.
Everytime you ask for the "make_person" key you get back that object.
You need to make the initialization of a Person lazy using a lambda.
factory = { "make_person" => lambda { Person.new } }

factory["make_person"].call

