# Immediate if method

**URL:** <https://www.ruby-forum.com/t/immediate-if-method/59536>\
**Category:** Ruby\
**Created:** [May 3, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/immediate-if-method/59536 "2006-05-03T23:00:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [May 3, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/immediate-if-method/59536/1 "2006-05-03T23:00:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [May 3, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/immediate-if-method/59536/2 "2006-05-03T23:06:30Z")

</div>

On Thu, May 04, 2006 at 05:59:22AM +0900, Jamal M. 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

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [May 3, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/immediate-if-method/59536/3 "2006-05-03T23:06:35Z")

</div>

true ? ‘true’ : ‘what’ going on?’

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [May 3, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/immediate-if-method/59536/4 "2006-05-03T23:09:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [May 3, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/immediate-if-method/59536/5 "2006-05-03T23:09:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [May 4, 2006, 1:12am UTC](https://www.ruby-forum.com/t/immediate-if-method/59536/6 "2006-05-04T01:12:19Z")

</div>

On May 3, 2006, at 2:03 PM, Gregory S. 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 H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
