From: Robert Klemme Date: 2004-06-15T04:58:36+09:00 Subject: Re: assignment in conditional warning "Rasputin" schrieb im Newsbeitrag news:pan.2004.06.13.13.09.19.96019@nowhere.net... > On Sat, 12 Jun 2004 16:36:27 +0200, Robert Klemme wrote: > > > > > "Rasputin" schrieb im Newsbeitrag > > news:pan.2004.06.11.20.50.29.369372@nowhere.net... > >> Why in the world is there a warning, *even with brackets*, for assignments > >> in conditionals. > > > > There are not always warnings; it seems, it's only warned if the right side > > is a literal (i.e. a string or a number). This is reasonable because it is > > likely that a comparison was meant. After all, what do you gain by doing > > > > if ( x = 10 ) > > end > > > > over > > > > if ( 10 ) > > end > > > > ? > > Actually, I don't think(not sure about this though) that Ruby actually > knows, whether the number/string/whatever is in the literal(?) form, or > if it's a return value from a function, beyond the parser level. It's sufficient to know that during parsing, because this is a syntax warning: $ ruby -c -e 'if x = 10; puts "ja"; end' -e:1: warning: found = in conditional, should be == Syntax OK > Therefore, checking for it should be hard (or better yet, a waste of > cycles). With that argument you'd have to do assembler only - every syntax and other check then is a waste of cycles. Apart from that: the check is only done once during compilation. There's no runtime overhead at all. > Personally, I think this warning should be allegated to the > $VERBOSE == true level. (Those would be much easier to ignore) I beg to differ. Rather write code that does not give you warnings. You should at least consider the option that there is actually a good reason for these warnings. > What I wanted to do was check if an array contained a certain value, and > do something with it, or else. Simple matter. How then did you get a warning? $ ruby -c -e 'a=%w{a b d c}; while a.include? "x"; p x; end' Syntax OK (no warning here) > Just a mite obfusticated, if at all. So I think the stuff above made > sense... (It was short and to the point anyway (i think...)) Which stuff? Did you post your code? I can't see it at the moment. Please show us the code; I bet we can then discuss this better. > But this is just going to be a matter that will be discussed my many > without anyone changing their minds (not really they won't). Well, I'm open to change my mind if you provide better arguments in favor of your position. It's just that I can't see them at the moment. > I mean should we go for short elegant code irritating-waste-of-spacers. I don't know what's irritating about x = get_x() if x == "foo" puts "ja" end I find this more irritating: if ( x = get_x() ) == "foo" puts "ja" end I prefer to have this only with loops because in that case it's a real gain in elegance that can't be achieved otherwise. But for if/unless it's plain superfluous. Regards robert