# Why is local variable created in one branch of if statement

**URL:** <https://www.ruby-forum.com/t/why-is-local-variable-created-in-one-branch-of-if-statement/91226>\
**Category:** Ruby\
**Created:** [April 5, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/why-is-local-variable-created-in-one-branch-of-if-statement/91226 "2007-04-05T19:50:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexeyv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@alexeyv](https://www.ruby-forum.com/u/alexeyv)\
**Post date:** [April 5, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/why-is-local-variable-created-in-one-branch-of-if-statement/91226/1 "2007-04-05T19:50:19Z")

</div>

I find it hard to understand why this code prints nil instead of  
saying “unknown method or local variable foo”:

> cat hmm.rb  
> def hmm  
> if false  
> foo = ‘quack’  
> else  
> p foo  
> end  
> end

hmm

> ruby hmm.rb  
> nil

Thoughts?

---

<div class="post-metadata">

**Author:** ![alexeyv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@alexeyv](https://www.ruby-forum.com/u/alexeyv)\
**Post date:** [April 5, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/why-is-local-variable-created-in-one-branch-of-if-statement/91226/2 "2007-04-05T20:04:20Z")

</div>

On Fri, 6 Apr 2007 02:49:27 +0900, “Alexey V.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> The decision on whether foo is a method or a variable is made by the  
> parser as it walks through the body of the function, rather than when  
> the function is executed. By default, foo is considered a method, but  
> once it encounters foo = somewhere, it considers foo to be a variable  
> for the remainder of the function.

So, by the time the parser processes the else branch of the if  
statement, it has already seen an assignment in the earlier if branch,  
and therefore interprets ‘foo’ as a variable name rather than a method  
call.

-mental

---

<div class="post-metadata">

**Author:** ![alexeyv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@alexeyv](https://www.ruby-forum.com/u/alexeyv)\
**Post date:** [April 5, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/why-is-local-variable-created-in-one-branch-of-if-statement/91226/3 "2007-04-05T20:11:20Z")

</div>

On Apr 5, 1:49 pm, “Alexey V.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> end
> 
> hmm\> ruby hmm.rb
> 
> nil
> 
> Thoughts?
> 
> –  
> Alex Verkhovsky

The variable ‘foo’ is created during parsing stage so exists as “foo =  
‘quack’” has been parsed.  
Execution then results in the assignment being skipped so foo has the  
default nil value.

Cheers  
Chris

---

<div class="post-metadata">

**Author:** ![alexeyv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/46a35a/32.png) [@alexeyv](https://www.ruby-forum.com/u/alexeyv)\
**Post date:** [April 5, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/why-is-local-variable-created-in-one-branch-of-if-statement/91226/4 "2007-04-05T20:54:28Z")

</div>

> The variable ‘foo’ is created during parsing stage so exists as “foo =  
> ‘quack’” has been parsed.  
> Execution then results in the assignment being skipped so foo has the  
> default nil value.

Thanks.
