Robert Klemme
7/29/2008 3:08:00 PM
2008/7/29 Pe=F1a, Botp <botp@delmonte-phil.com>:
> From: Ben Brightwell [mailto:neogia4@gmail.com]
> # The way I see it (and also the way I practice it today) is to always
> # always always use "&&" or "||" over "and" or "or", respectively.
> # The words "and" and "or" are lower in the order of operators than the
> # symbols "&&" and "||". This is intentional for beginner
> # programmers to be able to "read" the code rather than work out the
> # logic of the code.
>
> Ben thanks, but i'm not convinced. It still does not answer why it is not=
allowing in case-when clause yet allowing it in if-elsif,
The answer is the precedence defined into the syntax.
15:13:04 oz-27416_Failed_to_lock_accounts$ ruby -c <<XXX
> case
> when a > b || c < d
> puts 1
> end
> XXX
Syntax OK
17:05:28 oz-27416_Failed_to_lock_accounts$ ruby -c <<XXX
> case
> when a > b or c < d
> puts 1
> end
> XXX
-:2: syntax error, unexpected kOR, expecting kTHEN or ':' or '\n' or ';'
when a > b or c < d
^
-:4: syntax error, unexpected kEND, expecting $end
17:06:46 oz-27416_Failed_to_lock_accounts$
> sample,
>
> if 1=3D=3D1 and 2=3D=3D2
> p "ok"
> end
> "ok"
> #=3D> nil
>
> case
> when 1=3D=3D1 and 2=3D=3D2
> p "ok"
> end
> SyntaxError: compile error
> (irb):5: syntax error, unexpected kAND, expecting kTHEN or ':' or '\n' or=
';'
> when 1=3D=3D1 and 2=3D=3D2
> ^
> (irb):7: syntax error, unexpected kEND, expecting $end
> from (irb):7
> from :0
>
> arggh, i love using case-when and and/or and now both do not work togethe=
r?? quite a surprise to me there.
Note though that you can have your "or" differently in a "case" expression:
17:06:46 oz-27416_Failed_to_lock_accounts$ ruby -c <<XXX
> case
> when a > b, c < d
> puts 1
> end
> XXX
Syntax OK
17:08:01 oz-27416_Failed_to_lock_accounts$
And you can at least do
17:08:01 oz-27416_Failed_to_lock_accounts$ ruby -c <<XXX
> case
> when ( a > b and c < d )
> puts 1
> end
> XXX
Syntax OK
Kind regards
robert
--=20
use.inject do |as, often| as.you_can - without end