From: Jason Foreman Date: 2005-06-21T00:17:38+09:00 Subject: Re: x==1 vs 1==x On 6/20/05, Robert Klemme wrote: > Jason Foreman wrote: > > On 6/20/05, Florian Gro� wrote: > >> Jason Foreman wrote: > >> > >>> If you accidentally write x = 1 instead of x == 1, it can take a > >>> long time to figure out your bug. > >> > >> C:\Dokumente und Einstellungen\flgr>ruby > >> foo = 10 > >> if foo = 10 then > >> puts "Hello World!" > >> end > >> -:2: warning: found = in conditional, should be == > >> ^Z > >> Hello World! > >> > >> I found out about my bug before I was even done giving Ruby the full > >> application code. ;) > >> > >> > >> > > > > Nice. I didn't realize it gave that warning! :) > > > > However: > > > > $ ruby > > x=2 > > case x > > when x=1: p 'one' > > when x==2: p 'two' > > else p 'whoops!' > > end > > ^D > > "whoops!" > > Do you suggest to add a warning if there is an expression after "case" > *and* at least a single boolean expression after "when"? Note that the > correct form of what you did is this: > > x=2 > case > when x==1: p 'one' > when x==2: p 'two' > else p 'whoops!' > end > > Kind regards > > robert > > > Right, I should have used "case" and not "case x". Thanks for pointing this out. The corrected example of the problem I was trying to demonstrate is: $ ruby x=2 case when x=1: p 'one' when x==2: p 'two' end ^D "one" I'm not so sure you could easily add a warning for this, without crippling some of the capabilities of the language. Someone may very well want to do an assignment there. The probability of someone writing a case expression like that are probably very low though. It comes down to the fact that I like to write 1==x, because I made too many x=1 vs x==1 type of errors when I was learning to program and now its habit. Nobody else seems to agree with me though, so whatever :) Jason