← Back to Ruby Course | Chapter 8: Blocks, Procs & Lambdas | Lesson 2 of 6

yield with Arguments (Review)

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)

markup
def each_pair
  yield(1, "one")
  yield(2, "two")
end

each_pair { |num, word| puts "#{num}: #{word}" }
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.