Created
November 5, 2014 05:47
-
-
Save jeffreyiacono/aa90bdf52c440ab41080 to your computer and use it in GitHub Desktop.
fun with ruby blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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