i was just playing around with some ideas and came across something i could
not do although it seems to me that it wouldn’t be unreasonable to want to do
so:
def a_block_like_method(i)
puts i
end
arr = [1, 2, 3]
arr.each do a_block_like_method(i)
i also tried
def pass_a_block
{ |i| puts i }
end
arr = [1, 2, 3]
arr.each do pass_a_block
and i tried a few other variations with no success. is there a way to pass a
block or use a method as a block?
i was just playing around with some ideas and came across something i could
not do although it seems to me that it wouldn’t be unreasonable to want to do
so:
def a_block_like_method(i)
puts i
end
arr = [1, 2, 3]
arr.each do a_block_like_method(i)
But that's probably not what you want. It's not the method, it's a Proc
object (which happens to be the returned object from the method) that's
being iterated. You can use a local variable instead of a method:
pass_a_block = proc { |i| puts i }
arr = [1, 2, 3]
arr.each &pass_a_block
B.
···
On Tue, Feb 18, 2003 at 06:59:48AM +0900, Tom Sawyer wrote:
i was just playing around with some ideas and came across something i
could
not do although it seems to me that it wouldn’t be unreasonable to want
to do
so:
def a_block_like_method(i)
puts i
end
arr = [1, 2, 3]
arr.each do a_block_like_method(i)
i also tried
def pass_a_block
{ |i| puts i }
end
arr = [1, 2, 3]
arr.each do pass_a_block
and i tried a few other variations with no success. is there a way to
pass a