Hi~

On Dec 14, 2007, at 4:57 AM, Lloyd Linklater wrote:

> Chintana Wilamuna wrote:
>> Hi,
>>
>> If I declare a function that expects 3 arguments,
>>
>> def bar(x, y, z)
>>  ...
>> end
>>
>> and later, if I have the function name in a string, is there a way to
>> get the parameter names using reflection in Ruby?  Something similar
>> to the following PHP snippet,
>>
>> $a = new ReflectionFunction("function_name");
>> $a->getParameters();
>
> Forgive the naive thoughts of a ruby ruby, but what if you accepted an
> array as the parameter?  You could use any number of elements and you
> would always know which is which.
> --  
> Posted via http://www.ruby-forum.com/.
>



	You can accomplish this with the Ruby2Ruby gem. Here is a pastie of  
some code that can do this for you:

http://pastie.caboo.se/128959

	Here is how you can use it:

~/_merb/merb > irb -r get_args.rb
 >> class Foo
 >>   def hi(a,b,c)
 >>    p a,b,c
 >>   end
 >> end
=> nil
 >> Foo.instance_method(:hi).get_args
=> [[:a], [:b], [:c]]
 >>

Cheers-

- Ezra Zygmuntowicz
-- Founder & Software Architect
-- ezra / engineyard.com
-- EngineYard.com