Skip to content

Instantly share code, notes, and snippets.

@jbarnette
Forked from bmizerany/gist:760583
Created December 31, 2010 01:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarnette/760597 to your computer and use it in GitHub Desktop.
Save jbarnette/760597 to your computer and use it in GitHub Desktop.
# When anything, including nil, is a valid param and you need
# a clear way to know if a param was set or not.
def foo a, b = (b_was_not_passed = true; nil)
result = [a]
result << b unless b_was_not_passed
result
end
@jmhodges
Copy link

DEVIL.

@topfunky
Copy link

Wow. Just wow.

@djanowski
Copy link

Similar idea for when you receive an argument or a block:

def on(error, message = (block_given? ? yield : raise(ArgumentError)))

From https://github.com/soveran/ohm/blob/master/lib/ohm/validations.rb#L77-81

@rkh
Copy link

rkh commented Apr 1, 2011

if you dont care about the exception class, you can also do a def on(error, message = yield).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment