Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Beginner question: testing for either condition?

Reply
Thread Tools

Beginner question: testing for either condition?

 
 
Taylor Strait
Guest
Posts: n/a
 
      11-11-2006
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[erson_id]) or (session[erson_role]
== 'admin')

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

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

 
Reply With Quote
 
 
 
 
Nathan Witmer
Guest
Posts: n/a
 
      11-11-2006
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[erson_id]) ^
(session[erson_role] == 'admin')

 
Reply With Quote
 
 
 
 
Taylor Strait
Guest
Posts: n/a
 
      11-11-2006
> 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-forum.com/.

 
Reply With Quote
 
Hal Fulton
Guest
Posts: n/a
 
      11-11-2006
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[erson_id]) or (session[erson_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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unit testing beginner question Andrius Python 2 05-24-2011 12:19 AM
Beginner's Beginner william nelson Ruby 7 04-11-2011 11:23 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM
testing--news2004--testing Boomer Computer Support 3 09-24-2003 06:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57