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?

Spencer S. 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

Alex DeCaria wrote:

Spencer S. 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

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.

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 S. [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?

On Thu, Feb 18, 2010 at 4:17 AM, Spencer S. [email protected]
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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs