J
Jeff Davis
I know I can define a method like:
def foo(&p)
p.call
end
or:
def foo
yield
end
First, what's the difference between those two? I can pass a block to
either.
Also, how do I make the block optional? I would like to make a method
that performs it's task as usual, but you could also pass it a block
that it can use.
And also, can you pass more than one block to the same method? Is it
only the last argument?
And what's the difference between:
proc { puts 'foo'}
and:
{ puts 'foo' }
?
Is "proc" a keyword that turns the block into a value, or is the block
already a value and it's being passed to "proc" to turn it into a Proc
object?
Perhaps I'm not understanding blocks and iterators all that well.
Thanks,
Jeff Davis
def foo(&p)
p.call
end
or:
def foo
yield
end
First, what's the difference between those two? I can pass a block to
either.
Also, how do I make the block optional? I would like to make a method
that performs it's task as usual, but you could also pass it a block
that it can use.
And also, can you pass more than one block to the same method? Is it
only the last argument?
And what's the difference between:
proc { puts 'foo'}
and:
{ puts 'foo' }
?
Is "proc" a keyword that turns the block into a value, or is the block
already a value and it's being passed to "proc" to turn it into a Proc
object?
Perhaps I'm not understanding blocks and iterators all that well.
Thanks,
Jeff Davis