class

ArgumentError

ruby latest stable - Superclass: StandardError

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].first(4, 5)

raises the exception:

ArgumentError: wrong number of arguments (given 2, expected 1)

Ex: passing an argument that is not acceptable:

[1, 2, 3].first(-4)

raises the exception:

ArgumentError: negative array size

Files

  • error.c

1Note

Use this!

Soleone · Feb 11, 20092 thanks

You should raise your own +ArgumentError+ in methods to notify users of your class, if you think certain kinds of arguments aren't acceptable.

def transfer_money(amount)
unless amount.is_a?(Number)
  raise ArgumentError.new("Only numbers are allowed")
end
# ... Do the actual work
end