From: Regis d'Aubarede Date: 2010-06-03T22:28:46+09:00 Subject: dynamicaly define a method which body can contain yield Hello, So i try to define a methode by providing a bloc which can contain a yield. I got a error 'no block given (yield)' on yield execution Here is my test code : class B def p1 ; puts "p1"; end def p2 ; puts "p2 1" ; yield ; puts "p2 2" ; end def self.add_tag(name,&blk) puts "defined #{name}..." define_method(name) do instance_eval(&blk) end end def self.add_tag2(name,str_bloc) puts "defined2 #{name}..." module_eval %{ def #{name}() #{str_bloc} end } end end add_tag2 work well, but is not pretty : > B.add_tag2 "p4" "puts 'p4 '; p1 ; p2 { puts 'inp2' } ; yield" defined2 p4... => nil > b.p4 { puts 'in p4' } p4 p1 p2 1 inp2 p2 2 in p4 => nil add_tag give a Exception if bloc contain a yield: > B.add_tag("p5") { puts 'p5 '; yield } defined p5... => # > b.p5 { puts 'in p5' } p5 LocalJumpError: no block given from (irb):14 from ./essai.rb:7:in `instance_eval' from ./essai.rb:7:in `p5' from (irb):15 from :0 Is there a solution ? Thank you! Attachments: http://www.ruby-forum.com/attachment/4772/essai.rb -- Posted via http://www.ruby-forum.com/.