Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "wrong number of arguments (1 for 0)" mean in Ruby?

Tags:

ruby

What does "Argument Error: wrong number of arguments (1 for 0)" mean?

like image 652
Kotaro Ezawa Avatar asked Sep 24 '11 06:09

Kotaro Ezawa


People also ask

What does wrong number of arguments mean?

The number of arguments to a procedure must match the number of parameters in the procedure's definition. This error has the following causes and solutions: The number of arguments in the call to the procedure wasn't the same as the number of required arguments expected by the procedure.

What is argument error in Ruby?

Ruby's ArgumentError is raised when you call a method with incorrect arguments. There are several ways in which an argument could be considered incorrect in Ruby: The number of arguments (arity) is wrong. The value of the argument is unacceptable. The keyword is unknown when using keyword arguments.

What is an argument error in rails?

Raised when the arguments are wrong and there isn't a more specific Exception class. Ex: passing the wrong number of arguments [1, 2, 3].

What does wrong number of arguments mean in Google Sheets?

The #NUM! error is caused by an invalid argument in a formula in Google Sheets. Below is an example. Formula: =rate(75, -1500, -10000, 200000) Result: #NUM!


2 Answers

When you define a function, you also define what info (arguments) that function needs to work. If it is designed to work without any additional info, and you pass it some, you are going to get that error.

Example: Takes no arguments:

def dog end 

Takes arguments:

def cat(name) end 

When you call these, you need to call them with the arguments you defined.

dog                  #works fine cat("Fluffy")        #works fine   dog("Fido")          #Returns ArgumentError (1 for 0) cat                  #Returns ArgumentError (0 for 1) 

Check out the Ruby Koans to learn all this.

like image 178
bennett_an Avatar answered Oct 08 '22 12:10

bennett_an


You passed an argument to a function which didn't take any. For example:

def takes_no_arguments end  takes_no_arguments 1 # ArgumentError: wrong number of arguments (1 for 0) 
like image 20
icktoofay Avatar answered Oct 08 '22 13:10

icktoofay