# Different handling of local variables and instance variables

**URL:** <https://www.ruby-forum.com/t/different-handling-of-local-variables-and-instance-variables/114464>\
**Category:** Ruby\
**Created:** [September 12, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/different-handling-of-local-variables-and-instance-variables/114464 "2007-09-12T23:40:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tammo\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@Tammo\_T](https://www.ruby-forum.com/u/Tammo_T)\
**Post date:** [September 12, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/different-handling-of-local-variables-and-instance-variables/114464/1 "2007-09-12T23:40:31Z")

</div>

Hello,  
I somehow stumbled over a, how I think, strange behavior. When I try to  
handle a undefined variable in irb I get something like that:

irb(main):001:0\> !blubb  
NameError: undefined local variable or method `blubb’ for main:Object  
from (irb):1

But when I do the same with a undefined instance variable I get:  
irb(main):002:0\> !@blubb  
=\> true

I think that is somehow strange. Because of that I have overseen a  
mispelling in the instance variable name. Is that the wanted behavior?

Regards,  
Tammo

---

<div class="post-metadata">

**Author:** ![Tammo\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@Tammo\_T](https://www.ruby-forum.com/u/Tammo_T)\
**Post date:** [September 13, 2007, 7:39am UTC](https://www.ruby-forum.com/t/different-handling-of-local-variables-and-instance-variables/114464/2 "2007-09-13T07:39:05Z")

</div>

On Sep 12, 3:59 pm, Tammo T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think that is somehow strange. Because of that I have overseen a  
> mispelling in the instance variable name. Is that the wanted behavior?

Yup, it’s what I wanted.  
(Do you care if that’s what I wanted?)

---

<div class="post-metadata">

**Author:** ![Tammo\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@Tammo\_T](https://www.ruby-forum.com/u/Tammo_T)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/different-handling-of-local-variables-and-instance-variables/114464/3 "2007-09-17T13:23:37Z")

</div>

Sorry maybe I misformulated my question. I try it again. So my questions  
are  
a) What is the intention for the different handling? What are the  
benefits  
and purpose of it?  
b) Is it possible to change that behavior somehow, maybe with something  
like ‘strict’?

I have laready looked around and have seen, that it is the normal  
behavior.  
I should get used to always use the -w switch. I have also seen in  
[http://rubyquiz.com/quiz23.html](http://rubyquiz.com/quiz23.html)  
that you can enable warnings in the script itself with  
$VERBOSE = true

But of course I get only warnings for the “active code”. Now is the  
questions do I have alsway to use unit-testing and to take care to get  
the  
whole coverage or are there some kind of linter existing to check the  
whole  
code if there are some possible execution flows where some variables  
could  
be uninitialiyed? Propably that is relative complicated because of the  
dynamic nature of ruby, but I am not sure. I have tried to compare ruby  
and  
perl with respect to this. It seems, that perl recognize unizializes  
variables on toplevel also only when warnings activated. But I get  
somehow  
a error message when I used a unitialized variable inside a block:

use strict;  
use warnings;

$b = 0;  
if ( !$a ) {  
print “uses unitialized variable\n”;  
}

if ( $b ) {  
if ( !$c ){  
print “ignores that $c is uninitialized\n”;  
}  
}

Then I get the error message (it seems caused by strict):  
Global symbol “$c” requires explicit package name at chk\_var.pl line 11.  
Execution of chk\_var.pl aborted due to compilation errors.

What I do not understand to be honesty, because I have tried before that  
I  
get only a warning for the undefined $a.

Anyway, in ruby I get:

$VERBOSE = true

$b = false  
if !$a then  
print “uses unitialized variable\n”;  
end

if $b then  
if !$c then  
puts “ignores that $c is uninitialized”  
end  
end

And get only, when run, the following message:  
chk\_var.rb:4: warning: global variable `$a’ not initialized  
uses unitialized variable

What makes sense in so far, as the interpreter never comes to the point  
where !$C is evaluated. This is a artificial example. But my questions  
is,  
what is the recomemended and usual used method for catching such errors  
in  
the coding. Of course some possibility would be to have always unit  
tests  
with 100% percent coverage. But I do not know if that is always possible  
and maybe there is a simpler solution. And otherwise, if somebody knows,  
I  
would like to know what perl means with that error message.

I appreciate every feedback.

Best regards,  
Tammo
