Find arguments from Ruby method
I think most of us will know that you can define a method with the splat operator and then treat that as an array to your hearts content.
def my_method(*args)
puts args
end
However, what I didn't know how to do was to easily get the number of arguments from a method when you didn't define it, or when you don't want to use the splat operator.
class Example
def test(first, second, third='optional')
end
end
puts Example.method(:test).parameters
That will even show you if the param is required or not. Found on stack overflow of course.
Written by Matt Brewer
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#