ruby way to say this?

M

matt neuburg

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()?:eek:therThing()

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.
 
M

Max Lapshin

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()?:eek:therThing()

oneThing || otherThing
 
J

James Edward Gray II

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()?:eek:therThing()

oneThing || otherThing

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

James Edward Gray II
 
L

Louis J Scoras

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()?:eek:therThing()

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)
 
T

Tassilo Horn

(e-mail address removed) (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
 
J

James Edward Gray II

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()?:eek:therThing()

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
 
M

matt neuburg

Max Lapshin said:
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()?:eek:therThing()

oneThing || otherThing

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

Louis J Scoras

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
 
S

Simon Kröger

matt said:
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()?:eek:therThing()

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"]
 
D

David

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.
 
P

Phrogz

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:

Bah! You have to come up with the easy answer and spoil all of our fun,
don't you?
 
M

matt neuburg

Simon Kröger said:
matt said:
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()?:eek:therThing()

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

Thx, I feel better now! m.
 
A

ara.t.howard

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()?:eek:therThing()

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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top