Different handling of local variables and instance variables

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?


On Sep 12, 3:59 pm, Tammo T. [email protected] 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?)

Sorry maybe I misformulated my question. I try it again. So my questions
a) What is the intention for the different handling? What are the
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
I should get used to always use the -w switch. I have also seen in
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
whole coverage or are there some kind of linter existing to check the
code if there are some possible execution flows where some variables
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
perl with respect to this. It seems, that perl recognize unizializes
variables on toplevel also only when warnings activated. But I get
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
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”;

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

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
what is the recomemended and usual used method for catching such errors
the coding. Of course some possibility would be to have always unit
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,
would like to know what perl means with that error message.

I appreciate every feedback.

Best regards,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs