Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2012 00:06
Show Gist options
  • Save anonymous/4272885 to your computer and use it in GitHub Desktop.
Save anonymous/4272885 to your computer and use it in GitHub Desktop.
Making Ruby || / && chaining for non nil/false values nicer
class Object
# Usage:
# maybe_zero.self_unless(&:zero?) || zero_fallback
# # => either maybe_zero or zero_fallback (if maybe_zero was zero)
def self_unless
self unless yield(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment