[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Get formal parameter names of a method?

Jeff

8/14/2006 6:16:00 PM

Is there a way to reflect on a method to get the declard names of the
parameters?

class Equipment

def install(tool, packaging)
end

end

I can do this:

m = Equipment.new.method(:install)
m.arity # => 2

I want to somehow find out that the client code has declared the
parameters named 'tool' and 'packaging'.

Is this possible somehow? I feel like it should be, but I can't quite
figure it out.

Thanks!
Jeff

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

6 Answers

Robert Klemme

8/14/2006 6:25:00 PM

0

Jeff Cohen wrote:
> Is there a way to reflect on a method to get the declard names of the
> parameters?
>
> class Equipment
>
> def install(tool, packaging)
> end
>
> end
>
> I can do this:
>
> m = Equipment.new.method(:install)
> m.arity # => 2
>
> I want to somehow find out that the client code has declared the
> parameters named 'tool' and 'packaging'.
>
> Is this possible somehow? I feel like it should be, but I can't quite
> figure it out.

It's not possible without access to the source code or some major
interpreter patches.

Kind regards

robert

Austin Ziegler

8/14/2006 6:27:00 PM

0

On 8/14/06, Jeff Cohen <cohen.jeff@gmail.com> wrote:
> Is there a way to reflect on a method to get the declard names of the
> parameters?

Not in this version of Ruby. I don't know if it's planned.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

rubyfan

8/14/2006 6:50:00 PM

0

On 8/14/06, Jeff Cohen <cohen.jeff@gmail.com> wrote:
> Is there a way to reflect on a method to get the declard names of the
> parameters?
>
> class Equipment
>
> def install(tool, packaging)
> end
>
> end
>
> I can do this:
>
> m = Equipment.new.method(:install)
> m.arity # => 2
>
> I want to somehow find out that the client code has declared the
> parameters named 'tool' and 'packaging'.
>
> Is this possible somehow? I feel like it should be, but I can't quite
> figure it out.
>
> Thanks!
> Jeff
>

the only way you could do this now would be to use ParseTree:

http://rubyforge.org/projects/...

Maybe there will be a built-in way to do this in 2.0?

Phil

Kenosis

8/14/2006 8:19:00 PM

0


Phil Tomson wrote:
> On 8/14/06, Jeff Cohen <cohen.jeff@gmail.com> wrote:
> > Is there a way to reflect on a method to get the declard names of the
> > parameters?
> >
> > class Equipment
> >
> > def install(tool, packaging)
> > end
> >
> > end
> >
> > I can do this:
> >
> > m = Equipment.new.method(:install)
> > m.arity # => 2
> >
> > I want to somehow find out that the client code has declared the
> > parameters named 'tool' and 'packaging'.
> >
> > Is this possible somehow? I feel like it should be, but I can't quite
> > figure it out.
> >
> > Thanks!
> > Jeff
> >
>
> the only way you could do this now would be to use ParseTree:
>
> http://rubyforge.org/projects/...
>
> Maybe there will be a built-in way to do this in 2.0?
>
> Phil
Its a long shot but as a nasty hack you could read the original source
file in and search to locate the "def" of the method and then via a
regex extract the names of the arguments. This would likely be very
slow so you'd probably want to do it once and for all for all methods
of interest. But, as a circular problem, how would you know the names
of the methods of interest in advance? Well, that would have to be an
assumption I suppose.

Good Luck,

Ken

ptkwt

8/14/2006 11:21:00 PM

0

In article <1155586728.891984.325110@i3g2000cwc.googlegroups.com>,
Kenosis <kenosis@gmail.com> wrote:
>
>Phil Tomson wrote:
>> On 8/14/06, Jeff Cohen <cohen.jeff@gmail.com> wrote:
>> > Is there a way to reflect on a method to get the declard names of the
>> > parameters?
>> >
>> > class Equipment
>> >
>> > def install(tool, packaging)
>> > end
>> >
>> > end
>> >
>> > I can do this:
>> >
>> > m = Equipment.new.method(:install)
>> > m.arity # => 2
>> >
>> > I want to somehow find out that the client code has declared the
>> > parameters named 'tool' and 'packaging'.
>> >
>> > Is this possible somehow? I feel like it should be, but I can't quite
>> > figure it out.
>> >
>> > Thanks!
>> > Jeff
>> >
>>
>> the only way you could do this now would be to use ParseTree:
>>
>> http://rubyforge.org/projects/...
>>
>> Maybe there will be a built-in way to do this in 2.0?
>>
>> Phil
>Its a long shot but as a nasty hack you could read the original source
>file in and search to locate the "def" of the method and then via a
>regex extract the names of the arguments. This would likely be very
>slow so you'd probably want to do it once and for all for all methods
>of interest. But, as a circular problem, how would you know the names
>of the methods of interest in advance? Well, that would have to be an
>assumption I suppose.
>

If you're going to do that you might as well use ParseTree.

Phil

Gary Wright

8/15/2006 12:25:00 AM

0


On Aug 14, 2006, at 2:16 PM, Jeff Cohen wrote:
> Is there a way to reflect on a method to get the declard names of the
> parameters?
> [...]
> Is this possible somehow? I feel like it should be, but I can't quite
> figure it out.

What is the use case for this sort of thing? Off hand the only reason
I could see needing this would be for some sort of debugger/ide/
development
tool, in which case ParseTree might be a solution.


Gary Wright