[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: ruby way to say this?

Gavin Kistner

10/17/2006 7:49:00 PM

From: Louis J Scoras [mailto:louis.j.scoras@gmail.com]
> How about:
>
> def if_zero(exp,alt)
> exp == 0 ? alt : exp
> end
>
> x = 0
>
> if_zero( (x+=1) + 1, 7) # => 2
> if_zero( x-=1 , 8) # => 8
>
> So 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() )