From: Igor Pirnovar Date: 2012-10-20T07:47:24+09:00 Subject: Re: About define_method li shoubo wrote in post #1079862: > I want to define a method in kernel using Flattern Scope. > > Kernel.send :define_method, :each_event do > events.each_pair do |message, condition| > yield message, condition > end > end . . . > However, if I use send define_method method to define each_event, I > would met a exception: > in `each_event': no block given (LocalJumpError) > > I do not know why? if I do not use "send", the "yield" work well. Also, > I can define method as follows according to "meta programming ruby" > says: > > Kernel.send :define_method, :each_event do |&block| > events.each_pair do |message, condition| > block.call message, condition > end > end The problem is not the 'send', but rather the 'define_method'. Namely, 'define_method' does not allow you to specify a method body that expects a blok. If you need to dynamically create a method that accepts a block, you need to use the 'def method_name; ...; end' and the 'class_eval' (see: D. Flanagan's & Y. Matsumoto's book 'The Ruby Programming Language' page:275). So, your example should look something like the following: Kernel.class_eval do def each_event(events) events.each_pair do |message, condition| yield message, condition end end end events = { one: 1, two: 2, three: 3 } each_event(events) { |x, y| puts "#{x}, #{y}" } Regards, igor -- Posted via http://www.ruby-forum.com/.