简体   繁体   中英

Ruby: Unable to do math operations with two arguments

Please bear in mind that I am fairly new to Ruby. I am currently following a tutorial that is asking me to create a basic calculator. I need to create a Calculator class, that has the following methods; description, add, subtract, multiply and divide.

My initialize method can successfully take two numbers, but I can't seem to get the other methods working.

Here is my code:

class Calculator
  attr_accessor :x, :y

  def self.description
    "Performs basic mathematical operations"
  end

  def initialize(x, y)
    @x = x
    @y = y
  end

  def add(x, y)
    x += y.to_i
  end

  def subtract(x, y)
    x -= y.to_i
  end
end    

I am getting "wrong number of arguments (0 for 2)"

The code is correct, but it doesn't make a lot of sense. You are passing the values to the initializer, therefore I expect your code to be used as it follows

c = Calculator.new(7, 8)
c.add
# => 15

and it's probably the way you are calling it. However, this is not possible because you defined add() to take two arguments. Therefore, you should use

c = Calculator.new(7, 8)
c.add(1, 2)
# => 3

But then, what's the point of passing x and y to the initializer? The correct implementation is either

class Calculator
  attr_accessor :x, :y

  def self.description
    "Performs basic mathematical operations"
  end

  def initialize(x, y)
    @x = x.to_i
    @y = y.to_i
  end

  def add
    x + y
  end

  def subtract
    x - y
  end
end  

or more likely

class Calculator
  def self.description
    "Performs basic mathematical operations"
  end

  def initialize
  end

  def add(x, y)
    x.to_i + y.to_i
  end

  def subtract(x, y)
    x.to_i - y.to_i
  end
end  

Right now your code doesn't make a lot of sense. Your Calculator class initializes with two values, but you never use them. If you really want to initialize with values, your class should look more like this:

class Calculator
  attr_reader :x, :y

  def self.description
    "Performs basic mathematical operations"
  end

  def initialize(x, y)
    @x = x
    @y = y
  end

  def add
    x + y
  end

  def subtract
    x - y
  end
end

You would then run code like: Calculator.new(3, 5).add which would return 8 . You don't need an attr_accessor in this case, just an attr_reader .

Otherwise, you should not initialize with values at all like so:

class Calculator

  def self.description
    "Performs basic mathematical operations"
  end

  def add(x, y)
    x + y
  end

  def subtract(x, y)
    x - y
  end
end

Which you would call like Calculator.new.add(3, 5) returning 8 . This approach makes more sense to me, but it seems like the tutorial you are using expects the first approach.

Your current code also is using += and -= , but I'm not sure what you are trying to achieve with this. In your existing code they are pretty meaning less because you are operating on local variables, not your instance variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM