[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

get the name of a method's parameter

Le Huy

9/7/2006 6:36:00 AM

Does anyone know how to use reflection to get the name of a parameter of
a method ?
It is useful for documenting a class and doing editor with code
completion

--
Posted via http://www.ruby-....

4 Answers

Hal E. Fulton

9/7/2006 6:45:00 AM

0

Le Huy wrote:
> Does anyone know how to use reflection to get the name of a parameter of
> a method ?
> It is useful for documenting a class and doing editor with code
> completion

Unfortunately that is currently impossible.

I hope and expect this will be available in the future, but I
don't think it will be soon.


Hal


Logan Capaldo

9/7/2006 9:50:00 PM

0

On Sep 7, 2006, at 3:53 PM, Matt Todd wrote:
> def meth(argy)
>
>> p local_variables
>> end
>
>
> The problem with this solution is that it does not get the name of the
> variable that it was called with, just what it's assigned to (and not
> entirely useful, though definitely not useless). I think Le is
> asking about
> how to get the name of the variable where the value is being copied
> to argy
> from.
>
> M.T.

I don't think so, the OP said:

On Sep 7, 2006, at 2:35 AM, Le Huy wrote:
> It is useful for documenting a class and doing editor with code
> completion
>
> --
> Posted via http://www.ruby-....
>

which leaves me to believe he's interested in the parameter names for
the method definition. (It would be silly (well maybe just extreme)
to document every callsite).




Logan Capaldo

9/7/2006 10:39:00 PM

0


On Sep 7, 2006, at 6:30 PM, Matt Todd wrote:

>
>
> You may be right. Well, if so, then your solution works like a
> charm. If
> not, then there's no solution.
>

My solution? I had no solutions, just random commentary. ;) (And
there is of course the set_trace_func based solution combined with
reading the source file)

> M.T.


Sam Smoot

9/7/2006 11:20:00 PM

0

This is an ugly hack, and isn't very pretty in usage, plus it's got
lots of holes I'm sure, but:

module ParameterInspector
def self.included(base)
base.extend ClassMethods
end

module ClassMethods
def find_method_declaration(m)
cat[/def\s+#{m}($|\(.*)/]
end

def find_parameter(method_name, arity)
find_method_declaration(method_name)[/\(.*\)/][1...-1].split(',')[arity]
end

def cat
File.read(self.file)
end
end
end

Then to use it, you must write a class that defines a ::file method,
and include it.

class Example

include ParameterInspector

def self.file
__FILE__
end

def say(something)
puts something
end
end

puts Example.find_parameter :say, 0
"something"