[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

instance variable not initialized warning

Esad Hajdarevic

10/19/2006 9:30:00 AM

Hi!

In the following piece of code:

class Foo
def bar
if @var.nil?
puts "First assignment"
@var = 1;
else
@var +=1;
end
end
end

Foo.new.bar

When I run ruby (1.8.4)with -d ($DEBUG=true) I get to see the following
warning:

test.rb:3: warning: instance variable @var not initialized

Does it make sense to spit out this warning even if you are doing
something safe such as calling Object#nil?

What do you think?
1 Answer

Yoann Guillot

10/19/2006 11:18:00 AM

0

>
> Does it make sense to spit out this warning even if you are doing
> something safe such as calling Object#nil?
>
> What do you think?
>

The interpreter does not flag methods as 'safe' or 'unsafe'.

If you want to get rid of the warning, you can use

if defined? @var

or

@var ||= 0
@var += 1


Yoann