From: Derek Taylor Date: 2008-07-30T12:47:40+09:00 Subject: Re: and and or in case On Tue, 29 Jul 2008, Pea, Botp wrote: >Hi All, apologies in advanced if this has been discussed already > >why does ruby not compile the following case-when clause if using "and" >or "or" ? It works if i replace it with "&&" or "||", but i'm not asking >nor using that, "and/or" are much friendlier. The operators "and" and "&&" (and likewise "or" and "||") have different precedence levels. Things are binding differently than you think that they should be. > >samples, > >irb(main):038:0> case >irb(main):039:1* when 1==1 or 2==2 >irb(main):040:1> puts "ok" >irb(main):041:1> end >SyntaxError: compile error >(irb):39: syntax error, unexpected kOR, expecting kTHEN or ':' or '\n' or ';' >when 1==1 or 2==2 > ^ >(irb):41: syntax error, unexpected kEND, expecting $end > from (irb):41 > from ?:0 Try: >> case ?> when (1==1 or 2==2) >> puts "ok" >> end ok => nil >irb(main):050:0> case >irb(main):051:1* when 1==1 and 2==2 >irb(main):052:1> puts "ok" >irb(main):053:1> end >SyntaxError: compile error >(irb):51: syntax error, unexpected kAND, expecting kTHEN or ':' or '\n' or ';' >when 1==1 and 2==2 > ^ >(irb):53: syntax error, unexpected kEND, expecting $end > from (irb):53 > from :0 > >thank you and kind regards -botp > -Derek.