Bug #6132

Assignments don't happen correctly in "if" modifiers

Added by Ibrahim Tencer about 2 years ago. Updated about 2 years ago.

[ruby-core:43247]
Status:Rejected
Priority:Normal
Assignee:-
Category:-
Target version:-
ruby -v:ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.0.0] Backport:

Description

This works:

if lo = 3
la = lo
end

but this does not:

(la = lo) if (lo = 3)

It raises "NameError: undefined local variable or method `lo' for main:Object." But if you look at the values afterwards you get lo -> 3, la -> nil.


Related issues

Duplicated by ruby-trunk - Feature #8600: Recognize variables declared in if modifier at end of line Rejected 07/04/2013

History

#1 Updated by Eric Hodel about 2 years ago

  • Status changed from Open to Rejected

This is a feature of ruby.

Ruby parses left to right, so it see "lo" on the assignment side of the if. Since no local variable "lo" has been encountered yet it must be a method. Ruby then encounters the assignment to lo and creates the local variable.

Also available in: Atom PDF