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?
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 @@.
@@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@live.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Wed, February 17, 2010 9:56:14 PM
Subject: simple addition program, need help
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/.
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).
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.
On Thu, Feb 18, 2010 at 4:17 AM, Spencer Spence <spencaatee@live.com> wrote:
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.