Beginner question: testing for either condition?

T

Taylor Strait

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?
 
N

Nathan Witmer

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')
 
T

Taylor Strait

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!
 
H

Hal Fulton

Taylor said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top