I am at beginner level in Ruby, and an exercise I am working on requires me to create a Calculator class_ with various math methods.
Here is the code I've run, with the error. A hint I've been given in the course mentions @calc, but I don't know where or why to insert it.
class Calculator
attr_accessor :x, :y
def initialize(x,y)
@x, @y = x, y
end
def add()
x + y
end
def subtract() # **or should it be listed as y,x?**
y - x
end
def multiply()
x * y
end
def divide()
@x.to_f / @y.to_f
end
end
=> nil
calc = Calculator.new(5 , 2)
=> #<Calculator:0x00000101067258 @x=5, @y=2>
NoMethodError: undefined method `add' for #<Calculator:0x00000101067258 @x=5, @y=2>
from (irb):32
You have two errors.
First, there's a dot after the string (outside of it) in this line:
Should be:
And the other, you have a extra
endin your code. At the end of these lines:Should be: