# Introspection of method parameters

**URL:** <https://www.ruby-forum.com/t/introspection-of-method-parameters/85607>\
**Category:** Ruby\
**Created:** [March 22, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/introspection-of-method-parameters/85607 "2006-03-22T22:15:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [March 22, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/introspection-of-method-parameters/85607/1 "2006-03-22T22:15:09Z")

</div>

I’m trying to find out the number and name of a method’s parameters.  
I’ve googled for ‘ruby method parameter introspection’ and come up  
with almost nothing. Is there a way to ask a method for the  
parameters it takes?

Could this way also show me the method overloads?  
foo()  
foo(param1)  
foo(param1, param2)  
etc.

thanks,

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [March 22, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/introspection-of-method-parameters/85607/2 "2006-03-22T22:20:06Z")

</div>

Try this …

[http://www.rubycentral.com/book/ospace.html](http://www.rubycentral.com/book/ospace.html)

-Dave

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [March 22, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/introspection-of-method-parameters/85607/3 "2006-03-22T22:32:30Z")

</div>

Ok, I tried that and it gets me to where I am now, I can see the  
methods for an object, but I can’t tell if the method

a.) takes a parameter  
b.) if it takes a parameter the # of parameters it takes.

On 3/22/06, David I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Try this …
> 
> [http://www.rubycentral.com/book/ospace.html](http://www.rubycentral.com/book/ospace.html)
> 
> -Dave

> etc.

thanks,

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [March 22, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/introspection-of-method-parameters/85607/4 "2006-03-22T22:53:10Z")

</div>

On 3/22/06, Keith S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ok, I tried that and it gets me to where I am now, I can see the  
> methods for an object, but I can’t tell if the method
> 
> a.) takes a parameter  
> b.) if it takes a parameter the # of parameters it takes.

Both can be answered using the arity method in the class Method.

For example, how many parameters does the Math class method sin take?  
puts Math.method(:sin).arity  
outputs 1

How may parameters does the String instance method slice take?  
String.new.method(:slice).arity  
outputs -1  
This is because slice takes a variable number of arguments.  
In this case the return value is -n-1 where n is the number of  
required arguments.  
So the number of required arguments is zero.  
But I don’t think that’s true. It looks to me like slice requires 1  
argument!

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [March 22, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/introspection-of-method-parameters/85607/5 "2006-03-22T22:53:47Z")

</div>

On Thu, Mar 23, 2006 at 06:31:43AM +0900, Keith S. wrote:

> Ok, I tried that and it gets me to where I am now, I can see the  
> methods for an object, but I can’t tell if the method
> 
> a.) takes a parameter  
> b.) if it takes a parameter the # of parameters it takes.

RUBY\_VERSION # =\> “1.8.4”  
class Foo  
def foo(a); end  
def bar(a,b); end  
def baz(a,\*b); end # negative arity  
def nop; end  
end

%w[foo bar baz nop].each{|x| p [x, Foo.instance\_method(x).arity]}

# \>\> [“foo”, 1]

# \>\> [“bar”, 2]

# \>\> [“baz”, -2]

# \>\> [“nop”, 0]

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [March 22, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/introspection-of-method-parameters/85607/6 "2006-03-22T23:42:06Z")

</div>

On Thu, 2006-03-23 at 06:15 +0900, Keith S. wrote:

> I’m trying to find out the number and name of a method’s parameters.  
> I’ve googled for ‘ruby method parameter introspection’ and come up  
> with almost nothing. Is there a way to ask a method for the  
> parameters it takes?

I wrote a little thing [1](http://roscopeco.blogspot.com/2006/02/callcc-vs-catch.html) a while ago that lets you get the names of  
method arguments, though with some limitations (no C methods, no  
differentiation of block/splat args, etc).

> Could this way also show me the method overloads?  
> foo()  
> foo(param1)  
> foo(param1, param2)  
> etc.

Ruby doesn’t have method overloading.

---

<div class="post-metadata">

**Author:** ![ksader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@ksader](https://www.ruby-forum.com/u/ksader)\
**Post date:** [March 22, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/introspection-of-method-parameters/85607/7 "2006-03-22T22:54:17Z")

</div>

Try this …

class Test  
def initialize  
end

def one( a )  
end

def two( a, b )  
end  
end

t = Test.new  
m1 = t.method( :one )  
m2 = t.method( :two )

puts “Method one has " + m1.arity.to\_s + " parameters”  
puts “Method one has " + m2.arity.to\_s + " parameters”

=\> Method one has 1 parameters  
=\> Method two has 2 parameters

-Dave
