[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Getting method parameters in specified order

Chintana Wilamuna

12/13/2007 6:45:00 AM

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();

Thanks in advance,

Bye,

-Chintana

--
http://e...

8 Answers

bermonruf

12/13/2007 1:07:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

I think we cant do more than this:

irb(main):001:0> def test(a, b, c)
irb(main):002:1> end
=> nil
irb(main):003:0> method(:test).arity
=> 3

--
Bernardo Rufino

Chintana Wilamuna

12/13/2007 3:40:00 PM

0

On Dec 13, 2007 6:36 PM, Bernardo Monteiro Rufino <bermonruf@gmail.com> wrote:

> I think we cant do more than this:
>
> irb(main):001:0> def test(a, b, c)
> irb(main):002:1> end
> => nil
> irb(main):003:0> method(:test).arity
> => 3

Thanks Bernardo.

Bye,

-Chintana

--
http://e...

Phrogz

12/13/2007 5:51:00 PM

0

On Dec 12, 11:44 pm, Chintana Wilamuna <chinta...@gmail.com> 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?

There is not; as Bernardo said, Ruby only allows you to check the
arity of the method.

I had a need for this, too. (I was working on a project that allowed
developers to provide arbitrary Ruby-based plugins, and the GUI of the
application needed to expose the methods and their paramters
visually.)

For that need, I wrote this:
http://phrogz.net/RubyLibs/rdoc/classes/DescribeMe...

Peña, Botp

12/14/2007 3:22:00 AM

0

From: Chintana Wilamuna [mailto:chintanaw@gmail.com]=20
# Sent: Thursday, December 13, 2007 11:40 PM
# To: ruby-talk ML
# Subject: Re: Getting method parameters in specified order
#=20
# On Dec 13, 2007 6:36 PM, Bernardo Monteiro Rufino=20
# <bermonruf@gmail.com> wrote:
#=20
# > I think we cant do more than this:
# >
# > irb(main):001:0> def test(a, b, c)
# > irb(main):002:1> end
# > =3D> nil
# > irb(main):003:0> method(:test).arity
# > =3D> 3

not part of ruby itself, but try looking at ruby2ruby

kind regards -botp

Chintana Wilamuna

12/14/2007 8:14:00 AM

0

On Dec 14, 2007 8:52 AM, Pe=F1a, Botp <botp@delmonte-phil.com> wrote:

[snip]

Thanks Botp and Phrogz, I'll look into it.

Bye,

-Chintana

--=20
http://e...

Lloyd Linklater

12/14/2007 12:58:00 PM

0

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-....

Lloyd Linklater

12/14/2007 12:58:00 PM

0


> Forgive the naive thoughts of a ruby ruby...

ruby nuby, I meant. heh
--
Posted via http://www.ruby-....

Ezra Zygmuntowicz

12/14/2007 11:46:00 PM

0

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-....
>



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

http://pastie.caboo...

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