different handling of local variables and instance variables when undefined

T

Tammo Tjarks

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
 
P

Phrogz

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?)
 
T

Tammo Tjarks

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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top