# Nil or NameError

**URL:** <https://www.ruby-forum.com/t/nil-or-nameerror/100642>\
**Category:** Ruby\
**Created:** [February 3, 2006, 1:59pm UTC](https://www.ruby-forum.com/t/nil-or-nameerror/100642 "2006-02-03T13:59:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [February 3, 2006, 1:59pm UTC](https://www.ruby-forum.com/t/nil-or-nameerror/100642/1 "2006-02-03T13:59:25Z")

</div>

Hello,

(1…2).each { if 1 == 2 then a = 5 else puts a end }  
nil  
nil

(1…2).each { if 1 == 2 then b = 5 else puts a end }  
NameError: undefined local variable or method `a’ for main:Object  
from (irb):8  
from (irb):8

Okay, I understand what happend, but I am wondering. Is this okay? Is  
there any philosophical reason behind it or it’s just easier for the  
interpreter?

```
   Mage
```

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [February 3, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/nil-or-nameerror/100642/2 "2006-02-03T14:35:38Z")

</div>

On Fri, 2006-02-03 at 21:58 +0900, Mage wrote:

> Okay, I understand what happend, but I am wondering. Is this okay? Is  
> there any philosophical reason behind it or it’s just easier for the  
> interpreter?

I think it’s about the way Ruby decides whether ‘a’ is a method or a  
local variable. It uses a heuristic and looks for stuff like argument  
list and so forth, but in cases like this it comes down to whether Ruby  
has ‘seen an assignment’ to that symbol. Note that this doesn’t mean  
_executed_ an assignment, just seen (parsed? compiled?) one.

This is a simplified example:

puts a

# =\> -:2: undefined local variable or method `a’ for main:Object

if false then a = 5 end  
puts a

# =\> nil

In the first case, because Ruby hasn’t seen any previous reference to  
‘a’, the NameError is raised.

In the second case, I guess Ruby creates a local slot for ‘a’ as it  
compiles the if false… expression, initialised to nil, and since that  
is never executed, the local stays nil (but _is_ known as a local, Ruby  
‘remembers’ seeing it being assigned to, even though it was never  
executed).

---

<div class="post-metadata">

**Author:** ![Mage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@Mage](https://www.ruby-forum.com/u/Mage)\
**Post date:** [February 3, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/nil-or-nameerror/100642/3 "2006-02-03T15:11:56Z")

</div>

Ross B. wrote:

> > NameError: undefined local variable or method `a’ for main:Object  
> > I think it’s about the way Ruby decides whether ‘a’ is a method or a  
> > if false then a = 5 end  
> > executed).

I thought that it can be purposive. I have written a buggy code, which  
would either throw an exception or generate a bad result in other  
languages. However it works perfectly in Ruby.

While I never want to write inperfect code, I had to notice that Ruby’s  
scope and variable handling “fixed” my error.

```
      Mage
```
