Skip to content

Instantly share code, notes, and snippets.

Created January 22, 2014 17:48
Show Gist options
  • Save anonymous/043e3544b867482c9aae to your computer and use it in GitHub Desktop.
Save anonymous/043e3544b867482c9aae to your computer and use it in GitHub Desktop.
def foo(option, &block)
if block_given? bar(option, &block)
else
bar(option)
end
end
def bar(option, &block)
p option
block.call if block_given?
end
> foo(3)
3
# ArgumentError: wrong number of arguments(1 for 0)
#from (pry):25:in `block_given?'
> foo(3) { puts "tick" }
3
tick
#ArgumentError: wrong number of arguments(1 for 0)
#from (pry):25:in `block_given?'
def taz(option, &block)
p option
block.call if block_given?
end
taz(3) { puts "FOO" }
3
FOO
#=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment