R
Regis d'Aubarede
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 :
=> nil
p1
p2 1
inp2
p2 2
in p4
=> nil
add_tag give a Exception if bloc contain a yield:
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
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 :
defined2 p4...B.add_tag2 "p4" "puts 'p4 '; p1 ; p2 { puts 'inp2' } ; yield"
=> nil
p4b.p4 { puts 'in p4' }
p1
p2 1
inp2
p2 2
in p4
=> nil
add_tag give a Exception if bloc contain a yield:
defined p5...B.add_tag("p5") { puts 'p5 '; yield }
p5=> # said:b.p5 { puts 'in 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