Problem usning 'OR' operator in 'IF' condition?

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"

  end

Even when i pass a value for @character as ‘R’ its showing error like

Expected 'R' after 'E' for version Declaration (RuntimeError)

Please could any one explain in detail about the problem and how to
solve it when i have to use multiple check in the same if loop.

Thanks in advance

Regards,
Martin

Apply De Morgan’s and you’ll see this condition is always true:

@character != ‘R’ || @character != ‘r’

is equivalent to

!(@character == ‘R’ && @character == ‘r’)

As @character can never be both ‘R’ and ‘r’, @character == ‘R’ &&
@character == ‘r’ is always false, so the statement is

!(false)

which is true.

So your code is equivalent to

if true
raise " Expected ‘R’ after ‘E’ in version Declaration"
end

which is equivalent to

raise " Expected ‘R’ after ‘E’ in version Declaration"

You really mean something like

if @character != ‘R’ && @character != ‘r’
raise " Expected ‘R’ after ‘E’ in version Declaration"
end

Which is better done

if @character !~ /^r$/i
raise " Expected ‘R’ after ‘E’ in version Declaration"
end

If you use regular expressions, then you can probably save yourself a
lot
of unecessary conditionals by writing something like

if some_string !~ /^er$/i
raise " Expected ‘R’ after ‘E’ in version Declaration"
end

‘R’ != ‘r’, so the OR is true. If fact, it will always be true. You want
an
&& operator.

Mark

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

On Thu, Feb 14, 2008 at 1:01 AM, dare ruby [email protected]
wrote:

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

2008/2/14, dare ruby [email protected]:

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

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.

if @char != ‘R’ && @char != ‘r’ …
if ! ( @char == ‘R’ || @char == ‘r’ ) …

(btw, see Morgan’s Laws: De Morgan's laws - Wikipedia )

Here’s an alternative way

raise “…” unless /^r$/i =~ @character

Kind regards

robert

dare ruby wrote:

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

HTH,
Sebastian

dare ruby wrote:

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’