Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > different handling of local variables and instance variables when undefined

Reply
Thread Tools

different handling of local variables and instance variables when undefined

 
 
Tammo Tjarks
Guest
Posts: n/a
 
      09-12-2007
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


 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      09-13-2007
On Sep 12, 3:59 pm, Tammo Tjarks <(E-Mail Removed)> 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?)

 
Reply With Quote
 
 
 
 
Tammo Tjarks
Guest
Posts: n/a
 
      09-13-2007
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

Phrogz wrote:

> On Sep 12, 3:59 pm, Tammo Tjarks <(E-Mail Removed)> 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?)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can local function access local variables in main program? Sullivan WxPyQtKinter Python 10 11-08-2007 02:51 PM
method self.a= and a= create different instance variables yuesefa Ruby 4 07-25-2006 07:05 PM
Confusion over instance variables, different objects DJB Perl Misc 1 12-03-2005 11:46 PM
Static local variables inside instance members Seb C++ 1 04-14-2004 01:36 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments