# Newbie Question on Operator Precedence?

**URL:** <https://www.ruby-forum.com/t/newbie-question-on-operator-precedence/118800>\
**Category:** Ruby\
**Created:** [October 31, 2007, 1:28am UTC](https://www.ruby-forum.com/t/newbie-question-on-operator-precedence/118800 "2007-10-31T01:28:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Z\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecccb3/32.png) [@Z\_T](https://www.ruby-forum.com/u/Z_T)\
**Post date:** [October 31, 2007, 1:28am UTC](https://www.ruby-forum.com/t/newbie-question-on-operator-precedence/118800/1 "2007-10-31T01:28:59Z")

</div>

I need your help to understand the output of this program.

puts false or true #----\> output is false. OR does not get evaluated?

if false or true  
then puts ‘then’ #----\> output is ‘then’. OR gets evaluated!  
else puts ‘else’  
end

What’s the reason? Thanks for all the help, in advance.

ZT

---

<div class="post-metadata">

**Author:** ![Z\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecccb3/32.png) [@Z\_T](https://www.ruby-forum.com/u/Z_T)\
**Post date:** [October 31, 2007, 2:38am UTC](https://www.ruby-forum.com/t/newbie-question-on-operator-precedence/118800/2 "2007-10-31T02:38:58Z")

</div>

Z T wrote:

> What’s the reason?

The line:

> puts false or true

is equivalent to:

(puts false) or true

puts returns nil, so that statement is equivalent to:

nil or true

which evaluates to:

false or true

and the result of that is true–but since you didn’t save the result in  
a variable, it is discarded. Try this:

result = (puts false) or true  
p result

–output:–  
nil

That result may also be surprising. That’s because the statement:

result = (puts false) or true

is equivalent to

(result = (puts false)) or true

puts returns nil, which gets assigned to result, and the statement  
becomes:

result or true

which is equivalent to:

nil or true

which is equivalent to:

false or true

which evaluates to true–but since the result isn’t stored anywhere, it  
is discarded.

> if false or true

That statement is equivalent to:

if (false or true)

which evaluates to:

if true

---

<div class="post-metadata">

**Author:** ![Z\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/ecccb3/32.png) [@Z\_T](https://www.ruby-forum.com/u/Z_T)\
**Post date:** [October 31, 2007, 2:41am UTC](https://www.ruby-forum.com/t/newbie-question-on-operator-precedence/118800/3 "2007-10-31T02:41:35Z")

</div>

Z T wrote:

> if false or true  
> then puts ‘then’ #----\> output is ‘then’. OR gets evaluated!  
> else puts ‘else’  
> end
> 
> What’s the reason? Thanks for all the help, in advance.
> 
> ZT

for the if statement it gets evaluated so it is true and “then” is  
evaluated.

for puts flase or ture, I tried it. I got the same result of yours.  
However, I tried puts false and true. The output was false. I am not  
sure about this one.

I tried this and it gave me true

puts “#{false or true}”  
It gets evaluated.

Thanks
