simple addition program, need help

S

Spencer Spence

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?
 
A

Alex DeCaria

Spencer said:
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
 
A

Alex DeCaria

Alex said:
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
 
S

Spencer Spence

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.
 
G

Glenn

[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 <[email protected]>
To: ruby-talk ML <[email protected]>
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?
 
J

Jesús Gabriel y Galán

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.

That's because gets returns a string, and the + method of string
concatenates them:

irb(main):001:0> "2" + "2"
=> "22"
irb(main):002:0> 2 + 2
=> 4
irb(main):003:0> number = gets.to_i
2
=> 2
irb(main):004:0> number2 = gets.to_i
2
=> 2
irb(main):005:0> number + number2
=> 4

To convert a string to an integer check the to_i method:

http://ruby-doc.org/core/classes/String.html#M000787

Hope this helps,

Jesus.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top