Nested blocks having arguments in Ruby -


i have serious troubles understand blocks arguments. use such kind of ruby code:

foobar.foo |foo_arg|   bar |bar_arg|     define_method :hello!, foo_arg, bar_arg       "hello, #{foo_arg} , #{bar_arg}!"     end   end end  include foobar hello!(:alice, :bob) # => "hello, alice , bob!" 

and so, added thoses lines:

module foobar   def self.foo &foo_block     instance_eval &foo_block   end    def self.bar &bar_block     instance_eval &bar_block   end end 

but because arguments between pipes specials, i've got syntax error. help!

your module seems work correctly. issue usage of define_method. parameters foo_arg, , bar_arg need part of block passed define_method.

module foobar   def self.foo(&foo_block)     instance_eval &foo_block   end    def self.bar(&bar_block)     instance_eval &bar_block   end    foo |foo_arg|     bar |bar_arg|       define_method :hello! |foo_arg, bar_arg|         "hello, #{foo_arg} , #{bar_arg}!"       end     end   end end  include foobar hello!(:alice, :bob) # => "hello, alice , bob!" 

should want.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -