From: Alex DeCaria Date: 2010-02-18T12:12:36+09:00 Subject: Re: simple addition program, need help Alex DeCaria wrote: > Spencer Spence wrote: >> class Calculater >> def dothis(x,y) >> puts x+y >> end >> end >> >> object = Calculater.new >> @@number = gets >> @@number2 = gets >> object.doThis(number,number2) >> >> i'm getting this error, >> C:\Users\Spencer_2\Documents\NetBeansProjects\RubyApplication1\lib\new_main.rb:12: >> undefined local variable or method `number' for main:Object (NameError) >> >> I'm used to programming in java so I assumed that the doThis(x,y) >> parameters are just temporary variables in ruby. Am I wrong? > > The variable number is different than the variable @@number. number is > a local variable, whereas @@number is a class variable. So, number (and > number2) are undefined when you are calling the doThis method. > > How about just doing number = gets and number2 = gets, without the @@. > > -Alex An additional thing to keep in mind. gets returns the input as a string. To make them numbers you should do number = gets.to_i (for integers) or gets.to_f (for floats). --Alex -- Posted via http://www.ruby-forum.com/.