Strange wrong number of arguments (1 to 0) error?

I am having a problem defining a method. I have this code in my buy model:

def update_amount newamount
    self.total_amount = self.total_amount +newamount
end

and this code is in another place:

buy.update_amount(amount)

If I run the program, I get this error:

ArgumentError (wrong number of arguments (1 for 0)):
  app/models/buy.rb:18:in `update_amount'

Now, if I change for this (just try):

buy.update_amount

I get this error:

ArgumentError (wrong number of arguments (0 for 1)):
      app/models/buy.rb:18:in `update_amount'

I'm new to Ruby on Rails, so maybe this is easy.

+4
source share
2 answers
def update_amount(newamount)
  self.total_amount += newamount
end

This adds a new amount to the current attribute value total_amount.

You tried to pass newamountas an argument to your attribute self.total_amount.

+6
source

Pretty complicated mistake! Line:

self.total_amount = self.total_amount +newamount

interpreted by Ruby as:

self.total_amount = self.total_amount(+newamount)

, ArgumentError.

Ruby +newamount (.. , newamount), , total_amount , + . :

self.total_amount = self.total_amount + newamount

. , +=, @backpackerhh.

+13

Source: https://habr.com/ru/post/1533739/


All Articles