61

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

John Bachir
  • 22,495
  • 29
  • 154
  • 227
Kotaro Ezawa
  • 1,509
  • 3
  • 13
  • 11

4 Answers4

96

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.

nurettin
  • 11,090
  • 5
  • 65
  • 85
bennett_an
  • 1,718
  • 1
  • 16
  • 35
  • 1
    -1 `Cat.new("Fluffy")` does not work fine. It gives "uninitialized constant Cat", and `Cat().new("Fluffy")` gives "ArgumentError: wrong number of arguments (0 for 1)". – Andrew Grimm Sep 25 '11 at 23:43
  • 7
    Intended to be more of a simple visual representation to explain what an argument error is. But if you are that concerned, please fix it.
    The other answers may have been more technically valid, but probably not as helpful to someone asking something as elementary as "what is an ArgumentError?"
    – bennett_an Sep 26 '11 at 07:30
  • @bennett_an Thank you for providing a link to Ruby Koans, looks very interesting. – Igor V. Jul 31 '17 at 10:04
11

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)
icktoofay
  • 126,289
  • 21
  • 250
  • 231
2

I assume you called a function with an argument which was defined without taking any.

def f()
  puts "hello world"
end

f(1)   # <= wrong number of arguments (1 for 0)
Howard
  • 38,639
  • 9
  • 64
  • 83
0

If you change from using a lambda with one argument to a function with one argument, you will get this error.

For example:

You had:

foobar = lambda do |baz|
  puts baz
end

and you changed the definition to

def foobar(baz)
  puts baz
end

And you left your invocation as:

foobar.call(baz)

And then you got the message

ArgumentError: wrong number of arguments (0 for 1)

when you really meant:

foobar(baz)
justingordon
  • 12,553
  • 12
  • 72
  • 116