From: Lyle Johnson Date: 2004-01-14T02:56:36+09:00 Subject: Re: Warnings on assignments in conditionals? David Heinemeier Hansson wrote: > I consider the following statements to be perfectly legit (although > perhaps slightly ambigious) and Ruby does obey, but throws this > mandating warning. Am I just being paranoid or is Ruby forcing a style > upon me :)? A single equals sign does assignment, so a statement like: if a = 1 then true else false end assigns the value 1 to 'a', and then evaluates the "truthfulness" of 'a'. In Ruby, only 'false' and and 'nil' evaluate to false, so this statement will *always* evaluate to true (i.e. because 'a' is 1, and 1 is true). If you're wanting to check whether the current value of 'a' is 1, you need to use two equals signs instead, i.e. if a == 1 then true else false end This statement doesn't change the value of 'a' (as the previous statement did). It just compares its current value against another value (1) and returns true if they are equal. Apologies in advance if you already understand this distinction, but it is one that has tripped many a programmer, even those who should know better ;) If you indeed mean to assign the value to 'a' (as in the first example), you can probably avoid the warning from Ruby by breaking this into two lines, e.g. a = 1 if a then true else false end Hope this helps, Lyle