Jwan622 Jwan622 - 1 year ago 73
Ruby Question

is there a way limit the arguments passed to a Ruby method in the method signature?

Is there any way to create a method that accepts arguments only if the argument is either 1,2,3?

Is there any way to restrict the arguments passed to the method? Is there anything like:

def some_method(number: [1,2,3])

If I try to pass in
as the keyword argument above, it raises an error message?

Answer Source

No, there is nothing like that.

You can, of course, have if statements inside your method to check the arguments, and then if you find a problem raise an ArgumentError exception:

raise ArgumentError, "Number must be 1, 2, or 3."
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download