[Q] is it impossible to pass block into proc object?

M

makoto kuwata

Hi,

I have a question about proc object and block.

I tried the following code:

pr = proc do
yield "hom"
end

pr.call do |arg|
p arg
end

What I expected is:

$ ruby homhom.rb
hom

But I got:

$ ruby homhom.rb
homhom.rb:2:in `block in <main>': no block given (yield)
(LocalJumpError)
from homhom.rb:5:in `call'
from homhom.rb:5:in `<main>'
$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]

Is it impossible to pass block to proc object?
 
R

Robert Klemme

Hi,

I have a question about proc object and block.

I tried the following code:

pr = proc do
yield "hom"
end

pr.call do |arg|
p arg
end

What I expected is:

$ ruby homhom.rb
hom

But I got:

$ ruby homhom.rb
homhom.rb:2:in `block in<main>': no block given (yield)
(LocalJumpError)
from homhom.rb:5:in `call'
from homhom.rb:5:in `<main>'
$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0]

Is it impossible to pass block to proc object?

The only way I am aware of ATM is explicit:

irb(main):003:0> pr = lambda {|bl| bl.call "hom" }
=> #<Proc:0x829cd74@(irb):3 (lambda)>
irb(main):004:0> pr.call lambda {|x| p x}
"hom"
=> "hom"

Kind regards

robert
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top