Forum: Ruby Parameter names from reflection?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
455a3fcf178ca031184bdce52be09fa1?d=identicon&s=25 mercury542 (Guest)
on 2005-11-14 22:08
(Received via mailing list)
Is there a way to use reflection or some other method to return the
names of
the parameters a method uses?  I know that 'arity' will return the
number of
parameters, but I'm looking for the names of them.

Thanks in advance,
Charlie
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 leavengood (Guest)
on 2005-11-14 22:50
(Received via mailing list)
On 11/14/05, Charlie Squires <mercury542@yahoo.com> wrote:
> Is there a way to use reflection or some other method to return the names of
> the parameters a method uses?

No. Why do you need this?

Ryan
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-11-15 04:18
(Received via mailing list)
If you can get the binding from the start of the method you can look at
#local_variables.

T.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 drbrain (Guest)
on 2005-11-15 08:09
(Received via mailing list)
On Nov 14, 2005, at 1:48 PM, Ryan Leavengood wrote:

> On 11/14/05, Charlie Squires <mercury542@yahoo.com> wrote:
>> Is there a way to use reflection or some other method to return
>> the names of
>> the parameters a method uses?
>
> No. Why do you need this?

Actually, ParseTree can give you this information.

http://rubyforge.org/projects/parsetree
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 bob.news (Guest)
on 2005-11-15 12:58
(Received via mailing list)
Trans wrote:
> If you can get the binding from the start of the method you can look
> at #local_variables.

IMHO you can't do that without changing the method.  Charlie asked for a
solution using reflection so I think this is not an option for him.

Kind regards

    robert
455a3fcf178ca031184bdce52be09fa1?d=identicon&s=25 mercury542 (Guest)
on 2005-11-16 18:30
(Received via mailing list)
Thanks for the feedback. I need a way to do this without changing the
method
itself. I'm trying to get an external program a list of parameters from
a
Ruby source file. I suppose I could write a parse script.

"Robert Klemme" <bob.news@gmx.net> wrote in message
news:3tu0n5Fum4erU1@individual.net...
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 leavengood (Guest)
on 2005-11-17 04:21
(Received via mailing list)
On 11/16/05, Charlie Squires <mercury542@yahoo.com> wrote:
> Thanks for the feedback. I need a way to do this without changing the method
> itself. I'm trying to get an external program a list of parameters from a
> Ruby source file. I suppose I could write a parse script.

In that case use ParseTree like Eric suggested. Here is something that
probably does about what you want (sorry if I ruined your fun, but
this seemed like an interesting problem):

require 'enumerator'
require 'parse_tree'

class MethodParams
  attr_reader :parsed, :methods
  def initialize(klass)
    @parsed = ParseTree.new.parse_tree(klass)
    @methods = {}
    @parsed[0].select {|i| i.respond_to?(:[]) and i[0] == :defn}.each do
|i|
      if i[2][0] == :scope
        @methods[i[1]] = i[2][1][1][1..-1]
      end
    end
  end
end

if $0 == __FILE__
  if ARGV.length < 1
    puts "Usage: #$0 <files to analyze>"
    exit(1)
  end
  class_list = ObjectSpace.enum_for(:each_object, Class).to_a
  ARGV.each do |file|
    require file
  end
  new_classes = ObjectSpace.enum_for(:each_object, Class).to_a -
class_list
  new_classes.each do |klass|
    m = MethodParams.new(klass)
    m.methods.keys.each do |method|
      puts "The parameters for #{klass}##{method} are
#{m.methods[method].join(',')}"
    end
  end
end
__END__

Depending on your platform, you may have trouble with RubyInline and
ParseTree. I had to do some hackery on my Windows laptop to get the
above to work. Note that the above will only show the parameters of
instance methods on classes inside the required files.

Ryan
Bbd147034f04bc4f8f3c3652cd2a2194?d=identicon&s=25 sera (Guest)
on 2005-11-17 04:57
(Received via mailing list)
RDoc parses this info too, maybe you could dig around in RDoc and see
how they do it?
This topic is locked and can not be replied to.