What I Learned Today
I needed to introspect on a Ruby method and find out the names of its arguments for a project I'm working on, and realized Ruby's own introspection wouldn't let me get this info. After some Googling, I found that Merb has this functionality. I didn't want to include all of Merb in my project, so I've extracted it.
class Dummy
def one_arg(foo); end
def multiple_args(foo, bar, baz); end
def optional_args(foo, bar = 1); end
def star_args(*foo); end
end
Dummy.instance_method(:one_arg).get_args
# => [[[:foo]], []]
Dummy.instance_method(:multiple_args).get_args
# => [[[:foo], [:bar], [:baz]], []]
Dummy.instance_method(:optional_args).get_args
# => [[[:foo], [:bar, 1]], [:bar]]
Dummy.instance_method(:star_args).get_args
# => [[[%s[*foo]]], []]
You can get the gem from Gemcutter.
Published: