How do I get a list of the arguments passed to a method, preferably one that I can iterate through?
For example something like
def foo(a,b,c)
puts args.inspect
end
foo(1,2,3)
=> [1,2,3]
?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can always define a method that takes an arbitrary number of arguments:
This does exactly what you want, but only works on methods defined in such a manner.
The
*argsnotation means “zero or more arguments” in this context. The opposite of this is the splat operator which expands them back into a list, useful for calling other methods.As a note, the
*-optional arguments must come last in the list of arguments.