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

  2. On 04/22/2012 05:43 AM, makoto kuwata wrote:
    > 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
    Robert Klemme, Apr 22, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi
    Replies:
    3
    Views:
    1,932
    David Lozzi
    Jun 1, 2005
  2. NevilleDNZ
    Replies:
    9
    Views:
    441
    NevilleDNZ
    Aug 16, 2006
  3. Wolfgang Nádasi-Donner
    Replies:
    0
    Views:
    121
    Wolfgang Nádasi-Donner
    May 31, 2007
  4. Tristin Davis

    A cleaner way to pass a block or proc

    Tristin Davis, Jun 26, 2008, in forum: Ruby
    Replies:
    14
    Views:
    232
    Joel VanderWerf
    Jun 26, 2008
  5. Replies:
    5
    Views:
    256
    Michele Dondi
    Jun 30, 2006
Loading...

Share This Page