From: Florian Gross Date: 2004-12-21T18:57:05+09:00 Subject: Re: Dynamic Methods not Behaving as Expected Marc Merlin wrote: > Motivation: I would like to create an "imposter" class that supports the > definition of dynamically-defined methods. Once defined, these dynamic > methods should be invokable as though they were garden-variety > statically-defined methods. > > Problem: The dynamic methods implemented in the following code (and other > work-arounds I've tried) don't seem to exhibit the exact same behavior as > their static counterparts. In particular, blocks attached at the point of > method invocation don't seem to be detected. Unfortunately .define_method can not be used to define methods with blocks before Ruby 1.9 because blocks have no block slots. You can workaround that by doing this: create_wrapper = lambda do |name| instance_eval %{ def #{name}(*args, &block) #{name}_block(block, *args) end %} end define_method(:foo_block) { |block, *args| puts "foo!" } create_wrapper.call(:foo) It's not the most beautiful thing of the world but at least it can be abstracted away easily...