On Thu, May 18, 2006 at 02:20:17AM +0900, Wes Gamble wrote:
} Greg,
} 
} How does this
} 
} str.to_s.split("::").inject(Object) { |ns,name| ns.const_get(name) }
} 
} work?

Magic :-)

So suppose you have a string "Foo::Bar" and you want to retrieve the module
(or class, or other constant) Bar from the module (or class) Foo. (For
simplicity we will assume that both are modules.) First, we make sure it's
a string and split it on "::" which results in the array ["Foo", "Bar" ].
Now we use inject with its optional starting object.

For the first iteration of inject, ns = Object and name = "Foo" so the
value of ns.const_get(name) is Foo (the module). On the next iteration,
ns = Foo and name = "Bar" so the value of ns.const_get(name) is Bar (the
module). That's the end of the iteration, so the value of the entire line
is Bar, which is the value of the method call, which is what you wanted in
the first place.

} Wes
--Greg