Jwan622 Jwan622 - 1 month ago 7
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
4
as the keyword argument above, it raises an error message?

Answer

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."