No Method Error - Ruby Calculator -


based on ruby monk calculator exercise, trying build simple calculator can add , subtract:

class calculator    def add(a,b)    + b   end   def subtract(a,b)    - b   end end  puts "input first integer" = gets.chomp.to_i  puts "input second integer" b = gets.chomp.to_i  puts "add or subtract?" response = gets.chomp.downcase  if response == "add"    calculator.add(a,b) else response == "subtract"   calculator.subtract(a,b) end 

when run code, keep getting 'nomethoderror' - methods 'add' , 'subtract' undefined. don't understand why i'm getting error, , wondering if i'm calling method wrong.

you defined methods @ instance level, rather class level. either use

def self.add(a,b)   + b  end 

or create instance of calculator

calc = calculator.new calc.add(a,b) 

Comments