1

Is there a quick way to output the possible parameter names of a ruby method?

For example:

def my_method(param1, param2, param3, ...) # stuff end puts get_params("my_method") 

which would output something like:

param1 param2 param3 ... 

Thanks!

Oh, btw I'm on ree-1.8.7...

4

1 Answer 1

4

How about:

class A def my_method(param1, param2, param3) # stuff end end A.instance_method(:my_method).parameters # => [[:req, :param1],[:req, :param2],[:req, :param3]] A.instance_method(:my_method).parameters.collect { |p| p[1] } # => [:param1, param2, param3] 

And if you do it on the irb console, as in your example:

>> def my_method(param1, param2, param3) >> # stuff >> end => nil >> def get_params(method_name) >> self.class.instance_method(method_name.to_sym).parameters.collect { |p| p[1] }.each { |name| puts name } >> end => nil >> get_params(:my_method) param1 param2 param3 => [:param1, :param2, :param3] 

(Copy/pasted from my irb console.)

Sign up to request clarification or add additional context in comments.

4 Comments

It should be know that whilst this is the best answer, Method#parameters is 1.9 only
Exactly 1.9.2. Ruby 1.9.1 doesn't contain it (at least my version ;) )
I'm stuck using ree-1.8.7, which doesn't look like it has #parameters either. This is definitely a solution though, but is there a way without that method?
+1 .. just re-read the question.. he is asking for the names of the params :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.