why can't I access my instance variables?

D

Donglai Gong

I'm having trouble accessing instance variables directly. What's wrong
with this...

class Level1
def initialize
@var1='var1 set.'
end

def printvar1
@var1
end
end

l1=Level1.new
print l1.printvar1, "\n"
print l1.var1

I get the following output:

% ruby test.rb
var1 set.
test.rb:13: undefined method `var1' for #<Level1:0x23d5e8 @var1="var1
set."> (NoMethodError)

I'm running version 1.8.0 preview3 on OS X.

Donglai
 

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,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top