Ruby: Unable to do math operations with two arguments -
Please keep in mind that I am quite new to Ruby. I am currently following a tutorial which is asking me to make a basic calculator. I need to create a calculator class, which has the following methods; Description, add, subtract, multiply and divide.
My initial method can successfully take two numbers, but I can not seem to work in other ways.
Here is my code:
class calculator attr_accessor: x ,: y def self.description "performs basic mathematical operations" end def start (x, y) @ X = x @ y = y end DEF plus (x, y) x + = y.to_i end diff subtraction (x, y) x - = y.to_i end and The wrong number of arguments (0 for 2) is getting "
The code is correct, but this is very Do not understand. You are passing the initial values, so I hope that your code will be used as
c = calculator.new (7, 8) c.add # = & gt; 15 And perhaps the way you are calling it. However, this is not possible because you have defined add () to take two arguments. Therefore, you must
c = Calculator.new (7, 8) c.add (1, 2) # = & gt; Should use. 3 But then, what does it mean to pass the starter to x and y ? The correct implementation is either
class calculator attr_accessor: x ,: y def self.description "performs basic mathematical operations" end def initial (x, y) @ x = x.to_i @ y = Y.to_i end DEF plus x + y closing df minus x - y termination or more likely to be
class calculator def self.description "Basic Does mathematical operations "end def start end DEF plus (x, y) x.to_i + y.to_i subtract the enddef (x, y) x.to_i - y.to_i and end
Comments
Post a Comment