[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Beginner question: testing for either condition?

Taylor Strait

11/11/2006 12:54:00 AM

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?

--
Posted via http://www.ruby-....

4 Answers

Nathan Witmer

11/11/2006 1:12:00 AM

0

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

Taylor Strait

11/11/2006 1:22:00 AM

0

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


--
Posted via http://www.ruby-....

Paul Lutus

11/11/2006 1:24:00 AM

0

Taylor Strait 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.

--
Paul Lutus
http://www.ara...

Hal E. Fulton

11/11/2006 1:26:00 AM

0

Taylor Strait 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