Is block ready : block_given « Method « Ruby






Is block ready


#!/usr/bin/env ruby

def gimme
  if block_given?
    yield
  else
    puts "Oops. No block."
  end
  puts "You're welcome." # executes right after yield
end

gimme { print "Thank you. " } # => Thank you. You're welcome.

 








Related examples in the same category

1.The yield Statement using the block_given? method from Kernel.
2.If there's a block, pass in the file and close the file when it returns