Beginner question: testing for either condition?

Discussion in 'Ruby' started by Taylor Strait, Nov 11, 2006.

  1. 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-forum.com/.
    Taylor Strait, Nov 11, 2006
    #1
    1. Advertising

  2. 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')
    Nathan Witmer, Nov 11, 2006
    #2
    1. Advertising

  3. > 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/.
    Taylor Strait, Nov 11, 2006
    #3
  4. Taylor Strait

    Hal Fulton Guest

    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
    Hal Fulton, Nov 11, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    574
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. -
    Replies:
    12
    Views:
    692
    Remon van Vliet
    Jun 15, 2005
  3. Andreas Eibach
    Replies:
    7
    Views:
    406
    CBFalconer
    Nov 16, 2008
  4. Andrius

    Unit testing beginner question

    Andrius, May 23, 2011, in forum: Python
    Replies:
    2
    Views:
    184
    Roy Smith
    May 24, 2011
  5. Bill W.
    Replies:
    13
    Views:
    293
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page