From: Robert Klemme Date: 2008-07-30T00:08:20+09:00 Subject: Re: and and or in case 2008/7/29 Peņa, Botp : > 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 < case > when a > b || c < d > puts 1 > end > XXX Syntax OK 17:05:28 oz-27416_Failed_to_lock_accounts$ ruby -c < 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==1 and 2==2 > p "ok" > end > "ok" > #=> nil > > case > when 1==1 and 2==2 > p "ok" > end > SyntaxError: compile error > (irb):5: syntax error, unexpected kAND, expecting kTHEN or ':' or '\n' or ';' > when 1==1 and 2==2 > ^ > (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 together?? 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 < 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 < case > when ( a > b and c < d ) > puts 1 > end > XXX Syntax OK Kind regards robert -- use.inject do |as, often| as.you_can - without end