How about:
def if_zero(exp,alt)
exp == 0 ? alt : exp
endx = 0
if_zero( (x+=1) + 1, 7) # => 2
if_zero( x-=1 , 8) # => 8So your original would look like:
# if_zero(one_thing, other_thing)
Let's make that more OO-pretty (using a goofy name):
class Numeric
def or_if_zero( alternate_value )
self.nonzero? ? self : alternate_value
end
end
Which makes the code:
oneThing().or_if_zero( otherThing() )
···
From: Louis J Scoras [mailto:louis.j.scoras@gmail.com]