The standard Ruby reflection does not provide for getting method parameter names (at least afaik), and the only other solution that I found is using parsetree, which is “more clean” then this method, but requires another gem…
So here it comes Monkey-Style:
def methods_with_parameters(klass, path_to_file)
methods = klass.instance_methods(false).sort
lines = File.read(path_to_file)
#find params for methods
methods.map do |method|
lines.detect{|l|l=~/def #{method}(.*)$/}
parameters = $1.tr('()','').split(',').map{|p| p.gsub(/=.*/, '')}.reject(&:blank?).map(&:strip)
[method, parameters]
end
end

5 comments
Comments feed for this article
July 5, 2009 at 8:03
Ryan Davis
multiline args?
boom
July 5, 2009 at 9:20
pragmatig
yep, your right, it would only find the one on the first line, its not failsafe just a monkeypatch… *works-on-my-codebase*-approved
August 12, 2009 at 8:21
Macario
I had the same need and I solved using RubyParser in a very simple gem that provides named parameters for any method:
http://github.com/maca/arguments
August 12, 2009 at 8:21
Macario
Kudos, Ryan
August 12, 2009 at 10:02
pragmatig
hmm i dont see how this would give me the parameter names of e.g. def xxx(hello, foo=nil, bar=1) –> ['hello','foo', 'bar']