Skip to content

Instantly share code, notes, and snippets.

@ahirschberg
Created March 26, 2015 21:57
Show Gist options
  • Select an option

  • Save ahirschberg/e3e7aa5782b334025d9e to your computer and use it in GitHub Desktop.

Select an option

Save ahirschberg/e3e7aa5782b334025d9e to your computer and use it in GitHub Desktop.
yield statements in ruby
def foo
yield 1
yield 2
puts "* This code is inside #foo"
yield 3
puts "* This code is also inside #foo"
end
foo # Exception: no block given (yield) (LocalJumpError)
# from yield.rb:9
foo {|val| puts "> Value #{val} was passed into block"} #=>
# > Value 1 was passed into block
# > Value 2 was passed into block
# * This code is inside #foo
# > Value 3 was passed into block
# * This code is also inside #foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment