dynamicaly define a method which body can contain yield

  • Thread starter Regis d'Aubarede
  • Start date
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 :
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...
=> # said:
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
 
R

Robert Dober

Hello,

So i try to define a methode by providing a bloc which can contain a
yield.
This is known as the single biggest PITA of Ruby1.8, well by me.
If you can, use Ruby1.9 and you can do such wonderful things as
define_method :alpha do |beta, &gamma|
. .
gamma[...]
..
end

in 1.8. you have to do some nasty tricks with converting methods to
blocks or use simply "eval" or "module_eval" with a string.

HTH
R.
 
R

Regis d'Aubarede

Robert said:
in 1.8. you have to do some nasty tricks with converting methods to
blocks or use simply "eval" or "module_eval" with a string.

I try actualy with 1.9, but no solution by the way ...
 
R

Robert Dober

I =A0try actualy with 1.9, but no solution by the way ...
--
ok my bad, you call yield in the "client" block, that cannot work, let
us look at what yield really is

def implicit
yield # invoke the block provided by the client
end

def explicit &blk
blk[] # or blk.call, here it is clearer
end

now on the client side

implicit{ yield } # what shall be invoked here???

obviously
explicit{ blk[] }
cannot work as blk is the block you provide here, right.

Does this make your problem clearer?

HTH
R.

now if you call ex



--=20
The best way to predict the future is to invent it.
-- Alan Kay
 
R

Regis d'Aubarede

Here a (partial) solution :

class B
def p1 ; puts "p1"; end

def self.add_tag(name,&blk)
define_method(name) do |*args,&blocs|
instance_eval { blk.call(*args,&blocs) }
end
end

end

b=B.new
b.instance_eval {
B.add_tag("p5") { |*a,&bb| p1 ; bb.call(*a) ; p1 }
}

b.p5(1,2,3) {|*a| puts "in bloc p5 : #{a.inspect}" }
p1
in bloc p5 : [1, 2, 3]
p1

'b.instance_eval' seem necessary for that p1() call in add_tag() bloc
bind
to 'b' instance

Thank you ,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top