functions/yield_block_given.rb

tests/_examples/functions/yield_block_given.rb in the metorex repository.

def maybe_yield(x)
  if block_given?
    yield x
  else
    puts("no block: " + x.to_s)
  end
end

maybe_yield(42) do |n|
  puts("got: " + n.to_s)
end

maybe_yield(99)

Output:

got: 42
no block: 99