# Getting the name of arguments a method takes

**URL:** <https://www.ruby-forum.com/t/getting-the-name-of-arguments-a-method-takes/61960>\
**Category:** Ruby\
**Created:** [January 17, 2006, 5:00am UTC](https://www.ruby-forum.com/t/getting-the-name-of-arguments-a-method-takes/61960 "2006-01-17T05:00:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![didier-prophete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@didier-prophete](https://www.ruby-forum.com/u/didier-prophete)\
**Post date:** [January 17, 2006, 5:00am UTC](https://www.ruby-forum.com/t/getting-the-name-of-arguments-a-method-takes/61960/1 "2006-01-17T05:00:59Z")

</div>

I am wondering if there is a way to programatically get the name of the  
arguments a function take.

Say, I have this code:  
class MyClass  
def my\_fct(a, b, c)  
end  
end

Now, I can easily get a handle to the method ‘my\_fct’ using:  
mc = MyClass.new  
meth = mc.method(:my\_fct)

But how do I get to the list of arguments of ‘my\_fct’ ? I couldn’t find  
anything like:  
meth.arg\_list  
which would return [:a, :b, :c], or something like that.

Is this even possible in ruby ?

-Didier

---

<div class="post-metadata">

**Author:** ![didier-prophete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@didier-prophete](https://www.ruby-forum.com/u/didier-prophete)\
**Post date:** [May 1, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/getting-the-name-of-arguments-a-method-takes/61960/2 "2006-05-01T23:56:56Z")

</div>

Didier Prophete wrote:

> I am wondering if there is a way to programatically get the name of the  
> arguments a function take.
> 
> Say, I have this code:  
> class MyClass  
> def my\_fct(a, b, c)  
> end  
> end
> 
> Now, I can easily get a handle to the method ‘my\_fct’ using:  
> mc = MyClass.new  
> meth = mc.method(:my\_fct)
> 
> But how do I get to the list of arguments of ‘my\_fct’ ? I couldn’t find  
> anything like:  
> meth.arg\_list  
> which would return [:a, :b, :c], or something like that.
> 
> Is this even possible in ruby ?
> 
> -Didier

i also would like to do this. anybody know if it’s possible, and also  
getting arg names from a proc? if so it would be very handy for  
metaprogramming.

thanks

---

<div class="post-metadata">

**Author:** ![didier-prophete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@didier-prophete](https://www.ruby-forum.com/u/didier-prophete)\
**Post date:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/getting-the-name-of-arguments-a-method-takes/61960/3 "2006-05-03T18:59:00Z")

</div>

Guest wrote:

> > Now, I can easily get a handle to the method ‘my\_fct’ using:  
> > -Didier
> 
> i also would like to do this. anybody know if it’s possible, and also  
> getting arg names from a proc? if so it would be very handy for  
> metaprogramming.

The short answer: no, it’s not possible. What do you want it for?

There’s always this way, though:

def method\_arg\_names(method\_name, file)  
File.read(file)[/def #{Regexp.escape method\_name}(.\*()|$))/, 1]  
end

p method\_arg\_names(“method\_arg\_names”, $0) #=\> “(method\_name, file)”

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![didier-prophete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5daacb/32.png) [@didier-prophete](https://www.ruby-forum.com/u/didier-prophete)\
**Post date:** [May 3, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/getting-the-name-of-arguments-a-method-takes/61960/4 "2006-05-03T18:59:04Z")

</div>

On Tue, 2006-05-02 at 09:17 +0900, Dave B. wrote:

> > getting arg names from a proc? if so it would be very handy for  
> > p method\_arg\_names(“method\_arg\_names”, $0) #=\> “(method\_name, file)”
> 
> Cheers,  
> Dave

Also, this, if you’re only dealing with Ruby methods and are in a  
kamikaze kind of mood:

class Method  
def arg\_names  
catch(:traced) do  
dummy\_args = [nil]  
dummy\_args \*= self.arity unless self.arity \< 0

```
  set_trace_func lambda { |event, file, line, id, bdng, cls|
    if event == 'call'
      set_trace_func nil
      throw(:traced, eval('local_variables',bdng).map { |lv| 

```

lv.intern })  
end  
}

```
  self.call(*dummy_args)

  # if we get here, call is bust
  set_trace_func nil
  raise "No ruby method call in block"
end

```

end  
end

Doesn’t work for procs, though.
