Chapter 5 - Arrays and Blocks

Exercise 2: Yielding to a block

Write a method named with_these_numbers that takes two parameters, and yields them to a block.

Solution

def with_these_numbers(first_number, second_number)
  yield first_number, second_number
end

with_these_numbers(1, 4) do |first, second|
  puts first + second # Output: 5
end

with_these_numbers(3, 6) do |first, second|
  puts first * second # Output: 18
end

with_these_numbers(12, 3) do |first, second|
  puts first / second # Output: 4
end