[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Default block parameter?

Mark J. Reed

1/5/2006 12:51:00 AM

Okay, this is probably a dumb question, but how do I declare an
optional block parameter with a default value?

I tried several variations on this basic theme:

def meth(&block = lambda { |i| ... })
...
end

But I keep getting syntax errors. Help?
6 Answers

James Gray

1/5/2006 1:29:00 AM

0

On Jan 4, 2006, at 6:52 PM, Mark J.Reed wrote:

> Okay, this is probably a dumb question, but how do I declare an
> optional block parameter with a default value?
>
> I tried several variations on this basic theme:
>
> def meth(&block = lambda { |i| ... })
> ...
> end
>
> But I keep getting syntax errors. Help?

Hopefully this will give you some ideas:

>> def default
>> if block_given?
>> yield
>> else
?> lambda { puts "Default block" }.call
>> end
>> end
=> nil
>> default
Default block
=> nil
>> default { p 2 + 2 }
4
=> nil

James Edward Gray II



Paul Legato

1/5/2006 1:56:00 AM

0

Mark J.Reed wrote:
> Okay, this is probably a dumb question, but how do I declare an
> optional block parameter with a default value?
>
> I tried several variations on this basic theme:
>
> def meth(&block = lambda { |i| ... })
> ...
> end
>
> But I keep getting syntax errors. Help?
>

I think the & is your problem. You can do something like:

def foo(block = lambda {|i| puts i})
block.call("Hi")
end

foo()
foo(lambda {|i| puts i; puts i})

Best,
Paul



dblack

1/5/2006 2:13:00 AM

0

Jacob Fugal

1/5/2006 5:13:00 PM

0

On 1/4/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> On Thu, 5 Jan 2006, Mark J.Reed wrote:
>
> > Okay, this is probably a dumb question, but how do I declare an
> > optional block parameter with a default value?
> >
> > I tried several variations on this basic theme:
> >
> > def meth(&block = lambda { |i| ... })
> > ...
> > end
>
> The &block thing is a special dispensation from Ruby, letting you grab
> the block but not serving as a normal argument. The way I've always
> seen this done is:
>
> def meth(&block)
> block ||= lambda { ... }
> ...
> end

But keep in mind that assigning to block inside the method doesn't
affect the behavior of yield:

irb> def test(&block)
irb> block ||= lambda{ puts "default" }
irb> yield
irb> end
=> nil
irb> test
LocalJumpError: no block given

So if you need a default block and currently use yield, you'll either
need to branch on block_given? (as suggested by James), or just use
block.call instead of yield. The latter is probably preferrable, but
may have subtle differences in parameter assignment if it matters.

Jacob Fugal


dblack

1/5/2006 5:51:00 PM

0

Mark J. Reed

1/6/2006 2:58:00 PM

0

Thanks to all who posted. The upshot seems to be that I need to declare
the method with no block in the signature, and then check block_given?
within the body and manually invoke a default block if none was passed in.

Which is basically the solution I arrived at, although I had forgotten about
block_given? and was trapping the LocalJumpError to achieve the same result.

Thanks again!