# And in ternary operator

**URL:** <https://www.ruby-forum.com/t/and-in-ternary-operator/169526>\
**Category:** Ruby\
**Created:** [June 4, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/and-in-ternary-operator/169526 "2009-06-04T21:13:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 4, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/and-in-ternary-operator/169526/1 "2009-06-04T21:13:38Z")

</div>

hi,

i would like to do something like the following

if true  
do\_a  
else  
d\_a  
d\_b  
end

Can this be done using ternary operator?

This seem to give different result:  
ifTrue ? do\_a : do\_a and do\_b

Thank you.

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 4, 2009, 9:20pm UTC](https://www.ruby-forum.com/t/and-in-ternary-operator/169526/2 "2009-06-04T21:20:55Z")

</div>

Parv G. wrote:

> Can this be done using ternary operator?
> 
> This seem to give different result:  
> ifTrue ? do\_a : do\_a and do\_b
> 
> Thank you.

result = false ? nil : (x=1; x+=10; x)  
p result # ==\> 11

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 4, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/and-in-ternary-operator/169526/3 "2009-06-04T21:23:51Z")

</div>

On Thu, Jun 4, 2009 at 12:13 PM, Parv G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can this be done using ternary operator?
> 
> This seem to give different result:  
> ifTrue ? do\_a : do\_a and do\_b

If the goal is to always run the truthy condition and optionally some  
extra code for the falsey one, this will work as well:

do\_a ; do\_b if ifTrue

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 4, 2009, 10:54pm UTC](https://www.ruby-forum.com/t/and-in-ternary-operator/169526/4 "2009-06-04T22:54:44Z")

</div>

Parv G. wrote:

> hi,
> 
> i would like to do something like the following
> 
> if true  
> do\_a  
> else  
> d\_a  
> d\_b  
> end
> 
> Can this be done using ternary operator?
> 
> This seem to give different result:  
> ifTrue ? do\_a : do\_a and do\_b

‘and’ is very low precendence operator, and also will only do\_b if the  
result of do\_a is true.

ifTrue ? do\_a : (do\_a, do\_b)

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 5, 2009, 1:42am UTC](https://www.ruby-forum.com/t/and-in-ternary-operator/169526/5 "2009-06-05T01:42:37Z")

</div>

Hi,

At Fri, 5 Jun 2009 05:54:48 +0900,  
Brian C. wrote in [ruby-talk:338415]:

> ifTrue ? do\_a : (do\_a, do\_b)

You need a semicolon instead of a comma.

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 5, 2009, 2:23pm UTC](https://www.ruby-forum.com/t/and-in-ternary-operator/169526/6 "2009-06-05T14:23:21Z")

</div>

Nobuyoshi N. wrote:

> Brian C. wrote in [ruby-talk:338415]:
> 
> > ifTrue ? do\_a : (do\_a, do\_b)
> 
> You need a semicolon instead of a comma.

Sorry, that’s me with my C head on 🙂

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 4, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/and-in-ternary-operator/169526/7 "2009-06-04T21:30:55Z")

</div>

On Jun 4, 2009, at 3:13 PM, Parv G. wrote:

> Can this be done using ternary operator?
> 
> This seem to give different result:  
> ifTrue ? do\_a : do\_a and do\_b

since you always “do\_a”, why no execute it always?

do\_a  
do\_b if true

> 

regards,
