On Mar 24, 2004, at 12:52 PM, rolo wrote:

> I need something like this
>
> 	def on_success(&block)
> 		@event['success'] = block
> 	end
>
> I want the above method to be defined dynamically
>
> 	def define_my_method(symbol)
> 	....
> 	...
> 	....
> 	....
> 	end
>
> 	any call to define_my_method(:success) should result in
>
> 	def on_success(&block)
> 		@event['success'] = block
> 	end
>
> 	and will be used as
>
> 	myobject.on_success(){
> 		p "Success"
> 	}
> I tried using define_method but am having problems in passing a block.


It doesn't look like you can use #define_method, since blocks can't do 
the nifty &block conversion trick with their arguments. You may have to 
break down and use eval:

   def define_my_method(symbol)
     eval <<-EOC
       def on_#{symbol}(&block)
         @event['#{symbol}'] = block
       end
     EOC
   end

  >> b.define_my_method(:success)
=> nil
  >> b.instance_eval{@event={}}
=> {}
  >> b.on_success{p "Success"}
=> #<Proc:0x000615dc@(irb):27>
  >> b.instance_eval{@event['success'][]}
"Success"