Beginner question: testing for either condition?

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?

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’)

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!

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

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.