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

Discussion in 'Ruby' started by makoto kuwata, Apr 22, 2012.

  1. 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?
     
    makoto kuwata, Apr 22, 2012
    #1
    1. Advertisements

  2. 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
     
    Robert Klemme, Apr 22, 2012
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.