Gary Wright
3/15/2009 5:46:00 PM
On Mar 15, 2009, at 1:17 PM, matt neuburg wrote:
> This works but seems heavy-handed. I feel I should be able to do it
> without the anonymous class having any initialize method at all. The
> whole situation has "closure" written all over it. So I tried to
> define
> the hello method to accomplish the same thing using evals and blocks
> and
> other tricky stuff, and became hopelessly confused. Could someone
> straighten me out? Thx - m.
Here is one possibility. Note, I changed ClassMaker to a module
since it is really just a factory for creating classes and doesn't
really need to create intermediate instances of ClassMaker to
accomplish your goal.
module ClassMaker
def self.greeter(greeting)
Class.new do
define_method :hello do
puts greeting # this is the nub of the matter
end
end
end
end
ClassMaker.greeter("gday").new.hello
ClassMaker.greeter("aloha").new.hello
Gary Wright