ArgumentError
RubyERRORNotableArgument
Wrong number or type of arguments
Quick Answer
Check the method signature for required vs optional parameters and validate argument values before passing them.
What this means
Raised when a method receives the wrong number of arguments, or when an argument value is invalid for the method. It covers both arity mismatches and semantic validation failures.
Why it happens
- 1Passing too many or too few arguments to a method
- 2Passing an argument of the wrong value (e.g., negative where positive is required)
- 3Calling Integer() or Float() with a non-numeric string
Fix
Match the method arity
Match the method arity
def greet(name, greeting = 'Hello')
"#{greeting}, #{name}!"
end
greet('Alice') # OK
greet('Alice', 'Hi') # OK
# greet() # ArgumentError: wrong number of arguments (given 0, expected 1+)Why this works
Default parameters reduce required arity; keyword arguments with defaults are even more flexible.
Code examples
Arity mismatchruby
def add(a, b) a + b end add(1) # ArgumentError: wrong number of arguments (given 1, expected 2)
Invalid valueruby
Integer('abc')
# ArgumentError: invalid value for Integer(): "abc"Rescuing ArgumentErrorruby
begin Integer(params[:age]) rescue ArgumentError puts 'Age must be a valid integer' end
Same error in other languages
Sources
Official documentation ↗
Ruby Core Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev