Last active
August 29, 2015 14:20
-
-
Save bestie/4263cd398c666b726bd1 to your computer and use it in GitHub Desktop.
Proc behavior
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
# Proc behavior in reply to https://gist.github.com/jcoglan/0a37aee2582877b27920 | |
def foo(&block) | |
p [:foo, 1] | |
puts "block returns " + block.call.inspect | |
p [:foo, 2] | |
end | |
def bar(&block) | |
p [:bar, 1] | |
foo(&block) | |
p [:bar, 2] | |
end | |
def baz | |
p [:baz, 1] | |
puts "bar returns " + bar { | |
puts "always here" | |
next | |
raise "never happens" | |
}.inspect | |
p [:baz, 2] | |
end | |
baz | |
# => | |
# [:baz, 1] | |
# [:bar, 1] | |
# [:foo, 1] | |
# always here | |
# block returns nil | |
# [:foo, 2] | |
# [:bar, 2] | |
# bar returns [:bar, 2] | |
# [:baz, 2] | |
puts "========================================================================" | |
def baz_next_with_value(value) | |
p [:baz, 1] | |
puts "bar returns " + bar { | |
puts "always here" | |
next value | |
raise "never happens" | |
}.inspect | |
p [:baz, 2] | |
end | |
baz_next_with_value("the value") | |
# => | |
# [:baz, 1] | |
# [:bar, 1] | |
# [:foo, 1] | |
# always here | |
# block returns "the value" | |
# [:foo, 2] | |
# [:bar, 2] | |
# bar returns [:bar, 2] | |
# [:baz, 2] | |
puts "========================================================================" | |
def baz_next_with_break(value) | |
p [:baz, 1] | |
puts "bar returns " + bar { | |
puts "always here" | |
break value | |
raise "never happens" | |
}.inspect | |
p [:baz, 2] | |
end | |
baz_next_with_break("the value") | |
# => | |
# [:baz, 1] | |
# [:bar, 1] | |
# [:foo, 1] | |
# always here | |
# bar returns "the value" | |
# [:baz, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment