Skip to content

Instantly share code, notes, and snippets.

@bestie
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bestie/4263cd398c666b726bd1 to your computer and use it in GitHub Desktop.
Save bestie/4263cd398c666b726bd1 to your computer and use it in GitHub Desktop.
Proc behavior
# 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