Skip to content

Instantly share code, notes, and snippets.

@fronx
Created August 13, 2009 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fronx/167176 to your computer and use it in GitHub Desktop.
Save fronx/167176 to your computer and use it in GitHub Desktop.
class Object
# nil if false, else self
def only_if(&block)
self if self.instance_eval(&block)
end
end
puts ''.only_if { length > 0 } || 'previously blank'
# => 'previously blank'
puts 'not blank'.only_if { length > 0 } || 'previously blank'
# => 'not blank'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment