[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ruby way to say this?

matt

10/17/2006 7:12:00 PM

In Ruby, zero isn't false and there is no equivalent of the ?: operator
with the middle term omitted. So e.g. I'd like to say [pseudo-code from
some other language]:

oneThing()?:otherThing()

meaning, if oneThing is nonzero, return oneThing, else return
otherThing. Now, I don't want to evaluate oneThing twice, so I've ended
up with this:

(temp = oneThing()).nonzero? ? temp : otherThing()

I don't like it. Is that culturally correct, or is there a ruby way to
speak here, that I'm not thinking of? Thx - m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...
11 Answers

???? ??????

10/17/2006 7:22:00 PM

0


On Oct 17, 2006, at 11:15 PM, matt neuburg wrote:

> In Ruby, zero isn't false and there is no equivalent of the ?:
> operator
> with the middle term omitted. So e.g. I'd like to say [pseudo-code
> from
> some other language]:
>
> oneThing()?:otherThing()

oneThing || otherThing

James Gray

10/17/2006 7:26:00 PM

0

On Oct 17, 2006, at 2:21 PM, Max Lapshin wrote:

>
> On Oct 17, 2006, at 11:15 PM, matt neuburg wrote:
>
>> In Ruby, zero isn't false and there is no equivalent of the ?:
>> operator
>> with the middle term omitted. So e.g. I'd like to say [pseudo-code
>> from
>> some other language]:
>>
>> oneThing()?:otherThing()
>
> oneThing || otherThing

You didn't read that question all the way through. ;)

James Edward Gray II

Louis J Scoras

10/17/2006 7:45:00 PM

0

On 10/17/06, matt neuburg <matt@tidbits.com> wrote:
> In Ruby, zero isn't false and there is no equivalent of the ?: operator
> with the middle term omitted. So e.g. I'd like to say [pseudo-code from
> some other language]:
>
> oneThing()?:otherThing()
>
> meaning, if oneThing is nonzero, return oneThing, else return
> otherThing. Now, I don't want to evaluate oneThing twice, so I've ended
> up with this:
>
> (temp = oneThing()).nonzero? ? temp : otherThing()
>
> I don't like it. Is that culturally correct, or is there a ruby way to
> speak here, that I'm not thinking of? Thx - m.


Are you okay with adding an extra function call?

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)


--
Lou.

Tassilo Horn

10/17/2006 7:48:00 PM

0

matt@tidbits.com (matt neuburg) writes:

Hi Matt,

> meaning, if oneThing is nonzero, return oneThing, else return
> otherThing. Now, I don't want to evaluate oneThing twice, so I've
> ended up with this:
>
> (temp = oneThing()).nonzero? ? temp : otherThing()
>
> I don't like it. Is that culturally correct, or is there a ruby way to
> speak here, that I'm not thinking of?

What about this?

one_thing.nonzero? || other_thing

Bye,
Tassilo
--
* delYsid has mortgage, opportunity and penis in his score file.
<delYsid> thats pretty effective against spam
<Luke> aren't you worried about missing opportunities to mortgage
your penis?

James Gray

10/17/2006 7:50:00 PM

0

On Oct 17, 2006, at 2:44 PM, Louis J Scoras wrote:

> On 10/17/06, matt neuburg <matt@tidbits.com> wrote:
>> In Ruby, zero isn't false and there is no equivalent of the ?:
>> operator
>> with the middle term omitted. So e.g. I'd like to say [pseudo-code
>> from
>> some other language]:
>>
>> oneThing()?:otherThing()
>>
>> meaning, if oneThing is nonzero, return oneThing, else return
>> otherThing. Now, I don't want to evaluate oneThing twice, so I've
>> ended
>> up with this:
>>
>> (temp = oneThing()).nonzero? ? temp : otherThing()
>>
>> I don't like it. Is that culturally correct, or is there a ruby
>> way to
>> speak here, that I'm not thinking of? Thx - m.
>
>
> Are you okay with adding an extra function call?
>
> 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)

If you change that to support a block, you can avoid evaluating
other_thing unless it's needed:

def if_zero(exp)
exp.zero? ? yield : exp
end

if_zero(one_thing) { other_thing }

James Edward Gray II

matt

10/17/2006 7:53:00 PM

0

Max Lapshin <max@maxidoors.ru> wrote:

> On Oct 17, 2006, at 11:15 PM, matt neuburg wrote:
>
> > In Ruby, zero isn't false and there is no equivalent of the ?:
> > operator
> > with the middle term omitted. So e.g. I'd like to say [pseudo-code
> > from
> > some other language]:
> >
> > oneThing()?:otherThing()
>
> oneThing || otherThing

Please pretend I'm just staring at you waiting for you to see why that
won't work. :) m.


--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Louis J Scoras

10/17/2006 7:59:00 PM

0

On 10/17/06, James Edward Gray II <james@grayproductions.net> wrote:
>
> If you change that to support a block, you can avoid evaluating
> other_thing unless it's needed:
>
> def if_zero(exp)
> exp.zero? ? yield : exp
> end
>
> if_zero(one_thing) { other_thing }

Ahh, yes. That is a nice improvement. +1


--
Lou.

Simon Kröger

10/17/2006 8:00:00 PM

0

matt neuburg wrote:
> In Ruby, zero isn't false and there is no equivalent of the ?: operator
> with the middle term omitted. So e.g. I'd like to say [pseudo-code from
> some other language]:
>
> oneThing()?:otherThing()
>
> meaning, if oneThing is nonzero, return oneThing, else return
> otherThing. Now, I don't want to evaluate oneThing twice, so I've ended
> up with this:

Well, most of the time just ask ruby to do what you want:

oneThing.nonzero? || otherThing

--------------------------------------------------------------------------
num.nonzero? => num or nil

Returns num if num is not zero, nil otherwise. This behavior is useful when
chaining comparisons:

a = %w( z Bb bB bb BB a aA Aa AA A )
b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
--------------------------------------------------------------------------

cheers

Simon

David

10/17/2006 8:03:00 PM

0

On Wed, Oct 18, 2006 at 04:55:12AM +0900, matt neuburg wrote:
> Max Lapshin <max@maxidoors.ru> wrote:
> > oneThing || otherThing
>
> Please pretend I'm just staring at you waiting for you to see why that
> won't work. :) m.

zero is not false, so you would need

oneThing != 0 || otherThing

which, if oneThing was not equal to zero would return true, not the
value of oneThing.

--
David Dooling

matt

10/17/2006 8:18:00 PM

0

Simon Kröger <SimonKroeger@gmx.de> wrote:

> matt neuburg wrote:
> > In Ruby, zero isn't false and there is no equivalent of the ?: operator
> > with the middle term omitted. So e.g. I'd like to say [pseudo-code from
> > some other language]:
> >
> > oneThing()?:otherThing()
> >
> > meaning, if oneThing is nonzero, return oneThing, else return
> > otherThing. Now, I don't want to evaluate oneThing twice, so I've ended
> > up with this:
>
> Well, most of the time just ask ruby to do what you want:
>
> oneThing.nonzero? || otherThing
>
> --------------------------------------------------------------------------
> num.nonzero? => num or nil
>
> Returns num if num is not zero, nil otherwise.

Thx, I feel better now! m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...