Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created November 5, 2014 05:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jeffreyiacono/aa90bdf52c440ab41080 to your computer and use it in GitHub Desktop.
fun with ruby blocks
def something a, b, &block
sum = a + b
another_private_thing = "only something can see this"
puts "I'm in something, executing code."
puts "I added #{a} + #{b} and got #{sum}"
puts "and I can access my local vars: another_private_thing = #{another_private_thing}"
yield sum, another_private_thing
puts "now I'm after the block"
end
puts "starting up ..."
something 1, 2 do |sum_from_something, some_var|
puts "now I'm inside the block and have access to what something yielded to me!"
puts "can I access another_private_thing? => no \"#{some_var}\""
end
puts "finishing up ..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment