# Weird variable scope behaviour

**URL:** <https://rubytalk.org/t/weird-variable-scope-behaviour/40839>\
**Category:** ruby-talk\
**Created:** [22 September 2007 22:35 UTC](https://rubytalk.org/t/weird-variable-scope-behaviour/40839 "2007-09-22T22:35:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Just\_Another\_Victim1](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@Just\_Another\_Victim1](https://rubytalk.org/u/Just_Another_Victim1)\
**Post date:** [22 September 2007 22:35 UTC](https://rubytalk.org/t/weird-variable-scope-behaviour/40839/1 "2007-09-22T22:35:06Z")

</div>

I just wrote some code that shouldn't have worked, in my estimation, but  
it did, anyway. While this was a pleasant surprise, I prefer to understand  
why things don't work than to wonder why they did work...

&nbsp;&nbsp;&nbsp;&nbsp;What was happening is this situation, here:

if false  
&nbsp;&nbsp;&nbsp;&nbsp;variable = true  
end

if variable  
&nbsp;&nbsp;&nbsp;&nbsp;#do something  
end

&nbsp;&nbsp;&nbsp;&nbsp;To my surprise, this is okay! Even though the code in the first  
if-clause doesn't get run, the variable in the clause gets created,  
anyway...  
&nbsp;&nbsp;&nbsp;&nbsp;Now, I'm pretty sure this is expected behaviour. What I'm wondering is  
how people feel about this. This behaviour makes me feel uneasy. If I'm  
using a variable that hasn't been deliberately created then I'd rather have  
the interpreter complain that I haven't created the variable, as planned,  
than to be confused as to why my variable is nil.  
&nbsp;&nbsp;&nbsp;&nbsp;I'm very interested to hear other opinions on the subect. Anyone care  
to comment?  
&nbsp;&nbsp;&nbsp;&nbsp;Thank you...

---

<div class="post-metadata">

**Author:** ![7stud1](https://avatars.discourse-cdn.com/v4/letter/7/59ef9b/32.png) [@7stud1](https://rubytalk.org/u/7stud1)\
**Post date:** [22 September 2007 23:20 UTC](https://rubytalk.org/t/weird-variable-scope-behaviour/40839/2 "2007-09-22T23:20:54Z")

</div>

Just Another Victim wrote:

> if false  
> &nbsp;&nbsp;&nbsp;&nbsp;variable = true  
> end
> 
> if variable  
> &nbsp;&nbsp;&nbsp;&nbsp;#do something  
> end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;To my surprise, this is okay!

What do you mean by "okay"? Does the following not work as you would  
expect:

if false  
&nbsp;&nbsp;&nbsp;&nbsp;x = 10  
end

if x  
&nbsp;&nbsp;&nbsp;&nbsp;puts "yep"  
else  
&nbsp;&nbsp;&nbsp;&nbsp;puts "nope"  
end

Also, see here:

> **[Variable / Method Ambiguity](https://www.ruby-forum.com/t/variable-method-ambiguity/114592)**
>
> the following program for showing Variable / Method Ambiguity def a print “Function ‘a’ called\\n” 99 end for i in 1…2 if i == 2 print “a=”, a, “\\n” else a = 1 print “a=”, a, “\\n” end end will produce the result: a=1 Function ‘a’...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [23 September 2007 03:20 UTC](https://rubytalk.org/t/weird-variable-scope-behaviour/40839/3 "2007-09-23T03:20:04Z")

</div>

I understand the behavior, and I've learned to live with it. But (as  
I've noted on numerous occassions) I really don't like the reverse  
side effect, that you can't write:  
&nbsp;&nbsp;p foo if foo=true  
even though you can write:  
&nbsp;&nbsp;if foo=true then p foo; end

That two statements that are semantically identical should result in  
different behavior when foo has not previously been assigned a value  
is an unfortunate side effect/tradeoff of the implementation details  
and choices.

I accept it in the same way I accept that arrays and hashes have, in  
some cases, traded purity for speed. It ain't perfect, but it's what  
we got.

> **···**
>
> On Sep 22, 4:32 pm, "Just Another Victim of the Ambient Morality" \<ihates...@hotmail.com\> wrote:
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;I just wrote some code that shouldn't have worked, in my estimation, but  
> > it did, anyway. While this was a pleasant surprise, I prefer to understand  
> > why things don't work than to wonder why they did work...
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;What was happening is this situation, here:
> > 
> > if false  
> > &nbsp;&nbsp;&nbsp;&nbsp;variable = true  
> > end
> > 
> > if variable  
> > &nbsp;&nbsp;&nbsp;&nbsp;#do something  
> > end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;To my surprise, this is okay! Even though the code in the first  
> > if-clause doesn't get run, the variable in the clause gets created,  
> > anyway...  
> > &nbsp;&nbsp;&nbsp;&nbsp;Now, I'm pretty sure this is expected behaviour. What I'm wondering is  
> > how people feel about this. This behaviour makes me feel uneasy. If I'm  
> > using a variable that hasn't been deliberately created then I'd rather have  
> > the interpreter complain that I haven't created the variable, as planned,  
> > than to be confused as to why my variable is nil.  
> > &nbsp;&nbsp;&nbsp;&nbsp;I'm very interested to hear other opinions on the subect. Anyone care  
> > to comment?  
> > &nbsp;&nbsp;&nbsp;&nbsp;Thank you...

---

<div class="post-metadata">

**Author:** ![7stud1](https://avatars.discourse-cdn.com/v4/letter/7/59ef9b/32.png) [@7stud1](https://rubytalk.org/u/7stud1)\
**Post date:** [22 September 2007 23:23 UTC](https://rubytalk.org/t/weird-variable-scope-behaviour/40839/4 "2007-09-22T23:23:21Z")

</div>

7stud -- wrote:

> What do you mean by "okay"?

Ok, I see:

> **···**
>
> > This behaviour makes me feel uneasy. If  
> > I'm  
> > using a variable that hasn't been deliberately created then I'd rather  
> > have  
> > the interpreter complain that I haven't created the variable, as  
> > planned,  
> > than to be confused as to why my variable is nil.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud1](https://avatars.discourse-cdn.com/v4/letter/7/59ef9b/32.png) [@7stud1](https://rubytalk.org/u/7stud1)\
**Post date:** [22 September 2007 23:36 UTC](https://rubytalk.org/t/weird-variable-scope-behaviour/40839/5 "2007-09-22T23:36:02Z")

</div>

7stud -- wrote:

> Does the following not work as you would  
> expect:
> 
> if false  
> &nbsp;&nbsp;&nbsp;&nbsp;x = 10  
> end
> 
> if x  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "yep"  
> else  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "nope"  
> end

Ah. I see what you are concerned about:

if y  
&nbsp;&nbsp;&nbsp;&nbsp;puts "yep"  
else  
&nbsp;&nbsp;&nbsp;&nbsp;puts "nope"  
end

undefined local variable or method `y' for main:Object (NameError)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Just\_Another\_Victim1](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@Just\_Another\_Victim1](https://rubytalk.org/u/Just_Another_Victim1)\
**Post date:** [24 September 2007 02:10 UTC](https://rubytalk.org/t/weird-variable-scope-behaviour/40839/6 "2007-09-24T02:10:03Z")

</div>

"Phrogz" \<phrogz@mac.com\> wrote in message  
news:1190517508.484095.170340@k79g2000hse.googlegroups.com...

> I understand the behavior, and I've learned to live with it. But (as  
> I've noted on numerous occassions) I really don't like the reverse  
> side effect, that you can't write:  
> p foo if foo=true  
> even though you can write:  
> if foo=true then p foo; end
> 
> That two statements that are semantically identical should result in  
> different behavior when foo has not previously been assigned a value  
> is an unfortunate side effect/tradeoff of the implementation details  
> and choices.
> 
> I accept it in the same way I accept that arrays and hashes have, in  
> some cases, traded purity for speed. It ain't perfect, but it's what  
> we got.

&nbsp;&nbsp;&nbsp;&nbsp;I'm interested in to what you're referring, here. How was purity of  
arrays and hashes compromised for speed?  
&nbsp;&nbsp;&nbsp;&nbsp;Thank you...

---

<div class="post-metadata">

**Author:** ![Just\_Another\_Victim1](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@Just\_Another\_Victim1](https://rubytalk.org/u/Just_Another_Victim1)\
**Post date:** [24 September 2007 02:05 UTC](https://rubytalk.org/t/weird-variable-scope-behaviour/40839/7 "2007-09-24T02:05:04Z")

</div>

"7stud --" \<dolgun@excite.com\> wrote in message  
news:c84036c062811fa759eb9e680dfd75a6@ruby-forum.com...

> 7stud -- wrote:
> 
> > Does the following not work as you would  
> > expect:
> > 
> > if false  
> > &nbsp;&nbsp;&nbsp;&nbsp;x = 10  
> > end
> > 
> > if x  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "yep"  
> > else  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "nope"  
> > end
> 
> Ah. I see what you are concerned about:
> 
> if y  
> &nbsp;&nbsp;&nbsp;puts "yep"  
> else  
> &nbsp;&nbsp;&nbsp;puts "nope"  
> end
> 
> undefined local variable or method `y' for main:Object (NameError)

&nbsp;&nbsp;&nbsp;&nbsp;I'm not sure, exactly, what I'm concerned about but I can give you some  
clues.  
&nbsp;&nbsp;&nbsp;&nbsp;It's a little weird that the above code with variable y results in a  
name error but the previous example with variable x simply results in a nil  
object reference. It seems as if they should both result in a nil object  
reference or they should both result in a name error, rather than being  
inconsistent with each other.  
&nbsp;&nbsp;&nbsp;&nbsp;The most pragmatic example of a problem, that I can think of, is a  
situation where you have a bug that's immediately caused by a variable  
that's unexpectedly nil. Now, you have to trace back the life of the  
variable, which can traverse many different scopes, to hopefully discover  
that there was an execution path that didn't initialize the variable.  
Instead, wouldn't it have been nice if the interpreter simply told you, at  
the variable's first use, that it didn't exist 'cause it wasn't initialized?  
&nbsp;&nbsp;&nbsp;&nbsp;Now that I put it this way, I think I see how its current behaviour is  
an implementation detail, done for increasing speed, leaking through the  
interface...

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [24 September 2007 03:00 UTC](https://rubytalk.org/t/weird-variable-scope-behaviour/40839/8 "2007-09-24T03:00:07Z")

</div>

Things like the fact that Hash and Array literals don't call their  
#initialize method, or that all Array methods that fetch values from  
the array don't fall back on a single method that one can intercept or  
override.

> **···**
>
> On Sep 23, 8:05 pm, "Just Another Victim of the Ambient Morality" \<ihates...@hotmail.com\> wrote:
> 
> > "Phrogz" \<phr...@mac.com\> wrote in message  
> > \> I accept it in the same way I accept that arrays and hashes have, in  
> > \> some cases, traded purity for speed. It ain't perfect, but it's what  
> > \> we got.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;I'm interested in to what you're referring, here. How was purity of  
> > arrays and hashes compromised for speed?
