Default block parameter?

M

Mark J. Reed

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?
 
J

James Edward Gray II

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:
Default block
=> nil4
=> nil

James Edward Gray II
 
P

Paul Legato

Mark said:
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
 
D

dblack

Hi --

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?

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

I don't think there's a way to do it inside the arglist.


David

--
David A. Black
(e-mail address removed)

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
 
J

Jacob Fugal

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 ||=3D 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 ||=3D lambda{ puts "default" }
irb> yield
irb> end
=3D> 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
 
D

dblack

Hi --

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

Right -- all that happens in my version is assignment to a variable.


David

--
David A. Black
(e-mail address removed)

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
 

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,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top