Skip to content

Instantly share code, notes, and snippets.

@havenwood

havenwood/blocks.rb

Created Dec 4, 2020
Embed
What would you like to do?
Examples to illustrate what `yield` does in Ruby
##
# Examples without block arguments
def example_without_block(function)
function.call + 'ay'
end
example_without_block ->{ 'ok' }
#=> "okay"
def example_explicit_block(&block)
block.call + 'ay'
end
example_explicit_block { 'ok' }
#=> "okay"
def example_yield_block
yield + 'ay'
end
example_yield_block { 'ok' }
#=> "okay"
##
# Examples with one block argument
def example_without_block(function)
function.call('ay')
end
example_without_block proc { |ending| 'ok' + ending }
#=> "okay"
def example_explicit_block(&block)
block.call('ay')
end
example_explicit_block { |ending| 'ok' + ending }
#=> "okay"
def example_yield_block
yield('ay')
end
example_yield_block { |ending| 'ok' + ending }
#=> "okay"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment