[ruby-core:69061] [Ruby trunk - Bug #11115] [Open] Assigning a non-existent local variable to itself sets it to nil; unintuitive behaviour?

From: george@...
Date: 2015-05-03 06:17:06 UTC
List: ruby-core #69061
Issue #11115 has been reported by George Millo.

----------------------------------------
Bug #11115: Assigning a non-existent local variable to itself sets it to nil; unintuitive behaviour?
https://bugs.ruby-lang.org/issues/11115

* Author: George Millo
* Status: Open
* Priority: Low
* Assignee: 
* ruby -v: 2.2.2
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
Here's what normally happens when I try to reference a local variable that doesn't exist:

~~~
def foo
  bar
end

foo
# => NameError: undefined local variable or method `bar'
~~~

But if I assign bar to "itself", it doesn't raise an error

~~~
def foo
  bar = bar
  bar
end

foo
# => no results
~~~

Looks like `bar` in the second example is being set to `nil`. This doesn't feel very intuitive to me; shouldn't this raise an error? I mean, if you try to assign the non-existent variable 'bar' to anything else, that's how it works:

~~~
def foo
  fizz = bar
end

foo
# => NameError: undefined local variable or method `bar'
~~~

Is this a bug, or a feature?




-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next