You are getting a -1 because you are using local variables. Thus, the n
within your def is not the same as the n within your class.
Since you declared n to be -1 in your def, that is what your program
responded with, a -1.
You need to initialize an instance variable that will be available
throughout the class.
You can also make your code more utilitarian with a parameter value that
can be changed any time you create a new instance.
Try the following:
class Test
def initialize(n) @n = n
end
def pt
puts “hi there” @n -= 1
end
end
t = Test.new(100)
puts t.pt
This way, you are not tied down to the value 100, but can use any value
you want.
I am new at this game and just taking the baby steps, but this seemed to
work.
Lauren
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.