yield with Arguments (Review)
In this page:
yield with Arguments (Review)
A block can receive arguments from yield value just like a method call, letting the method feed data into the block's logic. This is exactly how methods like each pass the current element into your block. Multiple values can be yielded at once, matching multiple block parameters.
Note: The number of values yielded doesn't have to exactly match the block's parameter count -- extra block parameters simply become nil.
Example: yield with Arguments (Review)
def each_pair
yield(1, "one")
yield(2, "two")
end
each_pair { |num, word| puts "#{num}: #{word}" }
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: