From: Luke Burton Date: 2009-02-11T06:11:21+09:00 Subject: [ruby-core:21967] [Bug #1141] assignment of variable in "right" if statement fails Bug #1141: assignment of variable in "right" if statement fails http://redmine.ruby-lang.org/issues/show/1141 Author: Luke Burton Status: Open, Priority: Normal ruby -v: ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin10.0] In Ruby 1.8 >> p(x) if x=42 (irb):1: warning: found = in conditional, should be == NameError: undefined local variable or method `x' for main:Object from (irb):1 In Ruby 1.9 $ irb19 irb(main):001:0> p (x) if (x=42) NameError: undefined local variable or method `x' for main:Object from (irb):1 from /usr/local/bin/irb19:12:in `
' However, if you move the if statement to the line before, naturally everything works. Should variable assignment in "right" if clauses be permitted? I think so, since it allows you to be fairly concise and not have to do: if x = 42 p(x) end "Right" if clauses are permitted because they read naturally, I assume. I'm not sure whether people regard variable assignment in this case as reading very "naturally", but there is at the very least a lack of consistency between this type of if clause, and the more regular type of if clause. ---------------------------------------- http://redmine.ruby-lang.org