Hi,
I have a class called ClassFromString and it has a method called
getCounterValue. It takes a parameter called name. Is there in Ruby
using reflection to get the name of the parameters of a method.
class ClassFromString
@@counter = 0
def initialize
@@counter += 1
end
def getCounterValue(name)
puts name
end
end
def createClassFromString(classname)
ObjectSpace.each_object(Class) do |x|
if x.name == classname
object = x.new
for method in object.public_methods(false)=> Here I want to get the
parameters that are supported by the function
object.send(method,“Sirisha hi htere”)
end
end
end
end
createClassFromString(“ClassFromString”)
On Jun 16, 5:13 pm, Sirisha P. [email protected]
wrote:
I have a class called ClassFromString and it has a method called
getCounterValue. It takes a parameter called name. Is there in Ruby
using reflection to get the name of the parameters of a method.
There is not. The closest you can get is a hack that reads in source
files (if you know where they are, and they’re pure Ruby) and either
parses the raw code or looks at the abstract syntax tree.
Though I’m not actually recommending that you use it, this is why I
wrote my own DescribeMethods module:
http://phrogz.net/RubyLibs/rdoc/files/DescribeMethods_rb.html
(Seriously way more work than you want to do, I’m sure.)
Phrogz wrote:
On Jun 16, 5:13 pm, Sirisha P. [email protected]
wrote:
I have a class called ClassFromString and it has a method called
getCounterValue. It takes a parameter called name. Is there in Ruby
using reflection to get the name of the parameters of a method.
There is not.
That depends on what your definition of “is”
is.
You are wrong, in so far as Proc#parameters does exist in Ruby 1.9.2.
You are right, in so far as Ruby 1.9.2 doesn’t exist (yet).
See: https://GitHub.Com/RubySpec/MatzRuby/blob/trunk/proc.c#L694-710
The closest you can get is a hack that reads in source
files (if you know where they are, and they’re pure Ruby
… and they exist at all (i.e. the method was not created using eval
or define_method)
) and either
parses the raw code or looks at the abstract syntax tree.
Here is how the Merb-Action-Args Plugin for Merb does it:
JRuby:
https://GitHub.Com/WyCats/Merb/blob/master/merb-action-args/lib/merb-action-args/jruby_args.rb
(using JRuby’s own runtime reflection)
MRI:
https://GitHub.Com/WyCats/Merb/blob/master/merb-action-args/lib/merb-action-args/mri_args.rb
(using ParseTree)
For all others, it tries to use the MethoPara Gem, which, however,
only works on YARV 1.9.1 (and not on Ruby 1.9.1, as the README
incorrectly claims!): https://GitHub.Com/Genki/MethoPara/
jwm