Method Reflection

B

Benedikt Huber

I'm quite new to ruby and was amazed by the powerful reflection
mechanism, so tried to use the tracing function to analyse the object
space of our project.

It was amazingly easy to dump module,class and method definitions to xml
as they occured, but i was not able to retrieve any information about
the formal method parameters (correct term?), neither the number nor the
name. I wasn't able to retrieve information on instance variables as
well. Is it possible ?

If this kind of information retrieval is considered useful, I would also
suggest to add in-language comments as in python (especially if the irb
gets faster and more user friendly ;) )


ad emacs) Fixnum character literals mess up brace matching ( ?( ,?) ),
but this could be hard to fix. Is there a syntax-highlighting patch ?
 
E

Emmanuel Touzery

Benedikt said:
I'm quite new to ruby and was amazed by the powerful reflection
mechanism, so tried to use the tracing function to analyse the object
space of our project.

It was amazingly easy to dump module,class and method definitions to xml
as they occured, but i was not able to retrieve any information about
the formal method parameters (correct term?), neither the number nor the
name.
http://www.rubycentral.com/book/ref_c_method.html

arity gives the number of parameters (check the special cases in the doc
with method with variable number of parameters -with optional parameters-).

irb(main):001:0> a = String.new
=> ""
irb(main):002:0> a.method('split').arity
=> -1
irb(main):004:0> a.method('insert').arity
=> 2

i'm afraid you can't get the name of the parameters at runtime (this is
a job for rdoc).
I wasn't able to retrieve information on instance variables as
well. Is it possible ?

Apparently there is a instance_variables method.

emmanuel
 
G

gabriele renzi

but i was not able to retrieve any information about
the formal method parameters (correct term?), neither the number nor the
name. I wasn't able to retrieve information on instance variables as
well. Is it possible ?


arity() is your friend. About names: we'll see keyword arguments in
ruby2
If this kind of information retrieval is considered useful, I would also
suggest to add in-language comments as in python (especially if the irb
gets faster and more user friendly ;) )

probably yuo mean docstrings. I asked this once and matz answered that
this won't make it in ruby. But you can use the MetaTags module for
this (yes, ruby is impressively flexible :).
Or, post an RCR on rcrchive.net :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top