Skip to content

Instantly share code, notes, and snippets.

@ryanbriones
Created May 4, 2010 14:32
Show Gist options
  • Select an option

  • Save ryanbriones/389476 to your computer and use it in GitHub Desktop.

Select an option

Save ryanbriones/389476 to your computer and use it in GitHub Desktop.
def takes_an_optional_block(&block)
block_given_for_optional = block_given?
stash_block {
block_given_for_stash = block_given?
puts "block_given_for_optional: #{block_given_for_optional}"
puts "block_given_for_stash: #{block_given_for_stash}"
}
end
def stash_block(&block)
$block = block
end
def call_stashed
puts "block_given for call_stashed: #{block_given?}"
$block.call
end
takes_an_optional_block { "foo" }
call_stashed
puts "\nround 2:"
takes_an_optional_block
call_stashed
puts "\nround 3:"
takes_an_optional_block
call_stashed { "bar" }
block_given for call_stashed: false
block_given_for_optional: true
block_given_for_stash: true
round 2:
block_given for call_stashed: false
block_given_for_optional: false
block_given_for_stash: false
round 3:
block_given for call_stashed: true
block_given_for_optional: false
block_given_for_stash: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment