Forum: Ruby Immediate if method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2cf408af3f08d3575c9cd7697158a8f1?d=identicon&s=25 Jamal Mazrui (Guest)
on 2006-05-03 23:00
(Received via mailing list)
Does Ruby have a built-in equivalent of what some other languages call
an "immediate if" function?  I want to conditionally return one of two
strings on the status bar of an application, depending on whether a
variable is true or false.

So far, I have written the module method below, but I'm guessing there
is a more efficient and elegant way with Ruby.

Jamal

def if_string(condition, if_true, if_false)
if condition
return if_true
else
return if_false
end
end
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-05-03 23:06
(Received via mailing list)
On Thu, May 04, 2006 at 05:59:22AM +0900, Jamal Mazrui wrote:
} Does Ruby have a built-in equivalent of what some other languages call
} an "immediate if" function?  I want to conditionally return one of two
} strings on the status bar of an application, depending on whether a
} variable is true or false.
}
} So far, I have written the module method below, but I'm guessing there
} is a more efficient and elegant way with Ruby.

cond ? if_true : if_false

} Jamal
--Greg
8d16869783573d7ca80a676b65cf98e7?d=identicon&s=25 David Pollak (Guest)
on 2006-05-03 23:06
(Received via mailing list)
true ? 'true' : 'what\' going on?'
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-05-03 23:09
(Received via mailing list)
Maybe I don't know what you mean by "immediate if"...  meaning "if"
can be used as an expression?

If so, ruby can do this:

if condition then 3 else 5 end.times do { |x| puts x }

Or, more compactly:

(condition ? 3 : 5).times do { |x| puts x }

Or just:

if condition
   3
else
   5
end
573b9499030e1ccb867ef80f0ff1ac49?d=identicon&s=25 Justin Bailey (Guest)
on 2006-05-03 23:09
(Received via mailing list)
Just use the "ternary" if operator. In Ruby, all values except nil are
considered "true" in conditions. So you could write:

statusbar_text = condition ? "true text" : "false text"

The "if" statement actually "returns" a value, so a more verbose way
to write it would be:

statusbar_text = if condition
                           "true text"
                         else
                            "false text"
                         end

Hope that helps.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-05-04 01:12
(Received via mailing list)
On May 3, 2006, at 2:03 PM, Gregory Seidman wrote:

> } is a more efficient and elegant way with Ruby.
>
> cond ? if_true : if_false

Also,

result = if condition then true_value else false_value end

Sprinkle with newlines to taste.

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
This topic is locked and can not be replied to.