On Mon, Oct 15, 2012 at 8:37 AM, li shoubo <[email protected]> wrote:

> please help me to figure out why I can not use the first solution.

You simply cannot define methods which use block passed to them via
define_method as you have found out.  Minimalistic example:

$ ruby -e 'module Kernel;def foo; yield 1 end end; foo {|x| p x}'
1
$ ruby -e 'module Kernel;define_method(foo) { yield 1 };end;foo {|x| p x}'
-e:1:in `<module:Kernel>': undefined local variable or method `foo'
for Kernel:Module (NameError)
        from -e:1:in `<main>'

Another variant is to use class_eval:

$ ruby -e 'Kernel.class_eval "def foo; yield 1 end";foo {|x| p x}'
1

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to