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:
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]:
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:
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"]
--------------------------------------------------------------------------
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?
why not write in ruby what you are saying in english?
harp:~ > cat a.rb
def one_thing() 0 end
def other_thing() 1 end
p [one_thing, other_thing].detect{|thing| not thing.zero?}
def one_thing() 42 end
def other_thing() 0 end
p [one_thing, other_thing].detect{|thing| not thing.zero?}
harp:~ > ruby a.rb
1
42
??
-a
···
On Wed, 18 Oct 2006, 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:
You didn't read that question all the way through.
James Edward Gray II
···
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]:
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
···
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:
Please pretend I'm just staring at you waiting for you to see why that
won't work. m.
···
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()
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