Skip to content

Instantly share code, notes, and snippets.

@samsm
Last active March 31, 2019 22:36
Show Gist options
  • Save samsm/0fd54df9d50b00b5f5cb692bfbfec61d to your computer and use it in GitHub Desktop.
Save samsm/0fd54df9d50b00b5f5cb692bfbfec61d to your computer and use it in GitHub Desktop.
Couple examples of yield
# yield is like "do the thing that is in the block"
def do_twice
yield
puts "again!"
yield
end
do_twice { puts "hello world" }
do_twice do
puts "same thing, multiline syntax"
end
def times_two
2 * yield
end
times_two { 3 + 5 } # will return 16 (8*2)
times_two { 5 + 1 } # will return 12 (6*2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment