# How to do conditional operator?

**URL:** <https://www.ruby-forum.com/t/how-to-do-conditional-operator/85760>\
**Category:** Ruby\
**Created:** [February 16, 2007, 3:30am UTC](https://www.ruby-forum.com/t/how-to-do-conditional-operator/85760 "2007-02-16T03:30:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 16, 2007, 3:30am UTC](https://www.ruby-forum.com/t/how-to-do-conditional-operator/85760/1 "2007-02-16T03:30:41Z")

</div>

If an operator is just syntactic sugar for a message send, how do I  
coose an operator based on an expression evaluation?

For example:

if a  
s += “text”  
else  
s = “text”  
end

I should be able to write it something like this:

s ((a)? send("+=") : send("=")) “text”

Except the ruby compiler chokes on the syntax…

Joe

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 16, 2007, 3:41am UTC](https://www.ruby-forum.com/t/how-to-do-conditional-operator/85760/2 "2007-02-16T03:41:43Z")

</div>

Hi –

On Fri, 16 Feb 2007, removed\_email\_address@domain.invalid wrote:

> I should be able to write it something like this:
> 
> s ((a)? send("+=") : send("=")) “text”
> 
> Except the ruby compiler chokes on the syntax…

For several reasons 🙂 = isn’t a method, so you can’t send it to an  
object. When you do:

s = “whatever”

you are re-using the identifer ‘s’, not operating on the object to  
which it currently refers (if any). Also, remember that ‘send’ itself  
is a method. At some point, you have to use regular method-calling  
syntax (a dot, or falling back on the default receiver).

You could do the if/else version like this:

s = if a  
s + “text”  
else  
“text”  
end

(which could all actually be on one line, give or take a ‘then’) or,  
if you like the ternary thing:

s = a ? s + “text” : “text”

or even:

s = (a ? s : “”) + “text”

And then, somewhat more bizarrely, there’s:

s = “#{a&&s}text”

David

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 16, 2007, 3:53am UTC](https://www.ruby-forum.com/t/how-to-do-conditional-operator/85760/3 "2007-02-16T03:53:43Z")

</div>

On Fri, 16 Feb 2007, removed\_email\_address@domain.invalid wrote:

> I should be able to write it something like this:
> 
> s ((a)? send("+=") : send("=")) “text”
> 
> Except the ruby compiler chokes on the syntax…
> 
> Joe

harp:~ \> cat a.rb  
a, s = 42, ‘foo’  
s.send( a ? ‘\<\<’ : ‘replace’, ‘bar’ )  
p s

a, s = false, ‘foo’  
s.send( a ? ‘\<\<’ : ‘replace’, ‘bar’ )  
p s

harp:~ \> ruby a.rb  
“foobar”  
“bar”

-a
