Optional block arguments?

Discussion in 'Ruby' started by Vincent Foley, Nov 5, 2005.

  1. How can I have a block that optionally takes an argument? Thanks.

    Vincent.
     
    Vincent Foley, Nov 5, 2005
    #1
    1. Advertising

  2. Vincent Foley

    zdennis Guest

    Vincent Foley wrote:
    > How can I have a block that optionally takes an argument? Thanks.


    This has been involved in a recent discussion for how-to handle true anonymous functions in ruby.
    Currently you can't do this with proc or Procs, the same way you can with methods. A workaround is
    to use *args, since it will capture any/all parameters passed to the method and put them in an array:

    irb(main):013:0> pr = proc { |*args| args.length }
    => #<Proc:0xb7c8f154@(irb):13>
    irb(main):014:0> pr.call
    => 0
    irb(main):015:0> pr.call "hey"
    => 1
    irb(main):016:0> pr.call [ 1,2,3 ]
    => 1
    irb(main):017:0> pr.call [ 1,2,3 ], :key=>'val', :key2=>'val2'
    => 2

    Zach
     
    zdennis, Nov 5, 2005
    #2
    1. Advertising

  3. On 11/4/05, Vincent Foley <> wrote:
    > How can I have a block that optionally takes an argument? Thanks.


    irb(main):001:0> p =3D proc do |*a|
    irb(main):002:1* if a[0]
    irb(main):003:2> puts "Got parameter: #{a[0]}"
    irb(main):004:2> else
    irb(main):005:2* puts "No parameter"
    irb(main):006:2> end
    irb(main):007:1> end
    =3D> #<Proc:0x02b5d640@(irb):1>
    irb(main):008:0> p.call
    No parameter
    =3D> nil
    irb(main):009:0> p.call 1
    Got parameter: 1
    =3D> nil

    Ryan
     
    Ryan Leavengood, Nov 5, 2005
    #3
  4. Hi --

    On Sat, 5 Nov 2005, Ryan Leavengood wrote:

    > On 11/4/05, Vincent Foley <> wrote:
    >> How can I have a block that optionally takes an argument? Thanks.

    >
    > irb(main):001:0> p = proc do |*a|
    > irb(main):002:1* if a[0]


    I think you'd have to use a.size since with "if a[0]" you'll get false
    positives if the first arg is nil or false.


    David

    --
    David A. Black
     
    David A. Black, Nov 5, 2005
    #4
  5. On 11/5/05, Vincent Foley <> wrote:
    > How can I have a block that optionally takes an argument? Thanks.
    >
    > Vincent.

    I seem to remember Ara Howard recently answered this with an idiom
    something like this (apologies to Ara if I've remembered incorrectly):

    block =3D proc{|*args|
    a, b =3D *args
    a ||=3D 1
    b ||=3D 2
    p [a, b]
    }

    block.call
    block.call(99)
    block.call(99, 100)

    __END__
    [1, 2]
    [99, 2]
    [99, 100]
     
    Sean O'Halpin, Nov 5, 2005
    #5
    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. Matt
    Replies:
    6
    Views:
    782
  2. Paul Probert

    optional arguments in c extensions

    Paul Probert, Oct 24, 2003, in forum: Python
    Replies:
    1
    Views:
    273
    Paul Probert
    Oct 24, 2003
  3. Roger Upole
    Replies:
    0
    Views:
    424
    Roger Upole
    Dec 1, 2003
  4. morrell
    Replies:
    1
    Views:
    1,006
    roy axenov
    Oct 10, 2006
  5. Peter Motzfeldt
    Replies:
    1
    Views:
    176
Loading...

Share This Page