The yield Statement using the block_given? method from Kernel. : block_given « Method « Ruby






The yield Statement using the block_given? method from Kernel.


# the job of yield is to execute the code block that is associated with the method. 
 
def gimme
  if block_given?
    yield
  else
    puts "I'm blockless!"
  end
end

gimme { print "Say hi to the people." } # => Say hi to the people.

gimme # => I'm blockless!

 








Related examples in the same category

1.Is block ready
2.If there's a block, pass in the file and close the file when it returns