The statement " If character is not 'R' _OR_ character is not 'r' "
will always be true. If it is "R", then it is not "r", and vice
versa.
In the other one that you said works (with the == instead of the !=)
contradicts a little with your original code. Do you want to raise
when there exists an [rR] or when there does not exist an [rR]?
Look at Robert's code. I like the one with the regular expression.
Todd
···
On Thu, Feb 14, 2008 at 1:01 AM, dare ruby <martin@angleritech.com> wrote:
Dear friends,
I have got an error when i tried to check multiple condition using "OR"
operator inside "IF" loop.
my code is
if @character != 'R' || @character != 'r'
raise " Expected 'R' after 'E' in version Declaration"
Are you sure? From what I gather this logic is still flawed. I mean,
you want to throw if it is *neither* "r" *nor* "R". So you would need
to express that in your conditions, e.g.
but it works fine when the condition changes like,
if @character == 'R' || @character == 'r'
raise " Expected 'R' after 'E' in version Declaration"
end
Here you are saying: if character is 'R' or if it is 'r'. This condition
obviously can be true (if it is one of 'r' or 'R') or false (if it's
something else).
Previously you were saying: if character is something other than 'R' or it is
something other than 'r'. This will, as Paul pointed out, always be true.
Just think about: If character is 'R' than the first part won't be true ('R'
is not different from 'R'), but the second part will (because 'R' is
different from 'r'). If it is 'r', it's the other way around. And since the
whole expression is true when (at least) one of the parts is true (that's
what "or" means), it's always true. What you want is: !(@c == 'R' || @c=='r')
which is equivalent @c != 'R' && @c != 'r'. Note how here you have && instead
of ||. That's the law of DeMorgan that Paul mentioned:
!(a && b) <-> !a || !b
!(a || b) <-> !a && !b
Thank you paul and mark your informations have been very useful to me.
but it works fine when the condition changes like,
if @character == 'R' || @character == 'r'
raise " Expected 'R' after 'E' in version Declaration"
end
Regards,
Martin
Yep. When you change == to !=, you have to flip your ||s to &&s to get
the same effect.
"If the light is green OR the light is blue, the world is in danger"
becomes
"If the light is NOT green AND the light is NOT blue, the world is NOT
in danger"
P.S. Just use what Robert said unless regular expressions scare you. Or
do the whole
raise "Expected 'R' after 'E' in version declaration" unless @character.downcase == 'r'