From: Spencer Spence on
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?
--
Posted via http://www.ruby-forum.com/.

From: Alex DeCaria on
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
--
Posted via http://www.ruby-forum.com/.

From: Alex DeCaria on
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/.

From: Spencer Spence on
ah thank you, not totally sure why I made number a class variable. So
that's figured out but when i run it, if i type 2 and 2, it prints 2 2
instead of adding them like integers it adds them like strings.
--
Posted via http://www.ruby-forum.com/.

From: Glenn on
[Note: parts of this message were removed to make it a legal post.]

Spencer,

@@number and @@number2 are class variables. number and number2 are local variables. Also, Ruby is case sensitive, so the doThis method and the dothis method are actually 2 different methods.





________________________________
From: Spencer Spence <spencaatee(a)live.com>
To: ruby-talk ML <ruby-talk(a)ruby-lang.org>
Sent: Wed, February 17, 2010 9:56:14 PM
Subject: simple addition program, need help

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?
--
Posted via http://www.ruby-forum.com/.