# Beginner question: testing for either condition?

**URL:** <https://www.ruby-forum.com/t/beginner-question-testing-for-either-condition/75580>\
**Category:** Ruby\
**Created:** [November 11, 2006, 1:54am UTC](https://www.ruby-forum.com/t/beginner-question-testing-for-either-condition/75580 "2006-11-11T01:54:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 11, 2006, 1:54am UTC](https://www.ruby-forum.com/t/beginner-question-testing-for-either-condition/75580/1 "2006-11-11T01:54:02Z")

</div>

I want to check two conditions. If EITHER of them are true I want to  
proceed. Unfortunately what I have is returning true no matter what:

if (@unit.custodian\_id == session[:person\_id]) or (session[:person\_role]  
== ‘admin’)

How can I test for EITHER condition without repeating myeself? Is my  
only solution to use elsif?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 11, 2006, 2:12am UTC](https://www.ruby-forum.com/t/beginner-question-testing-for-either-condition/75580/2 "2006-11-11T02:12:04Z")

</div>

It’s a little unclear, but it sounds like you’re looking for an  
exclusive or. Looks like the bitwise XOR operator works on  
expressions, not just bits:

false ^ false # =\> false  
false ^ true # =\> true  
true ^ false # =\> true  
true ^ true # =\> false  
(1==1) ^ (1==2) # =\> true

So you should be able to do:

if (@unit.custodian\_id == session[:person\_id]) ^  
(session[:person\_role] == ‘admin’)

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 11, 2006, 2:22am UTC](https://www.ruby-forum.com/t/beginner-question-testing-for-either-condition/75580/3 "2006-11-11T02:22:20Z")

</div>

> false ^ false # =\> false  
> false ^ true # =\> true  
> true ^ false # =\> true  
> true ^ true # =\> false

That will ALMOST work. I also want true OR true =\> true. Basically if  
ANYTHING is true I want to proceed. Thanks for the reply!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 11, 2006, 2:28am UTC](https://www.ruby-forum.com/t/beginner-question-testing-for-either-condition/75580/4 "2006-11-11T02:28:09Z")

</div>

Taylor S. wrote:

> I want to check two conditions. If EITHER of them are true I want to  
> proceed. Unfortunately what I have is returning true no matter what:
> 
> if (@unit.custodian\_id == session[:person\_id]) or (session[:person\_role]  
> == ‘admin’)
> 
> How can I test for EITHER condition without repeating myeself? Is my  
> only solution to use elsif?

I don’t see what is wrong here. Are you sure your data are  
exactly what you think?

Trying rewriting as an else and see if it does what you  
expect… and we’ll tell if in fact the code is  
equivalent…

Hal

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [November 11, 2006, 2:26am UTC](https://www.ruby-forum.com/t/beginner-question-testing-for-either-condition/75580/5 "2006-11-11T02:26:06Z")

</div>

Taylor S. wrote:

> I want to check two conditions. If EITHER of them are true I want to  
> proceed. Unfortunately what I have is returning true no matter what:
> 
> if (@unit.custodian\_id == session[:person\_id]) or (session[:person\_role]  
> == ‘admin’)

In English: IF A=TRUE OR B=TRUE THEN ACT

Yes? Is this what you want? If not, offer a better explanation. But  
let’s  
assume yes.

In Ruby (note the change):

# if (@unit.custodian\_id == session[:person\_id] || session[:person\_role]

‘admin’)

# do something here

end

> How can I test for EITHER condition without repeating myeself? Is my  
> only solution to use elsif?

Avoid using a mixture of “&&, ||” and “and, or”. They have different  
orders  
of precedence. I prefer &&, ||, but that is a personal taste issue.
