public
Last active

  • Download Gist
snippet.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12
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'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.