# Getting argument names and default values: doable (if hackis

**URL:** <https://www.ruby-forum.com/t/getting-argument-names-and-default-values-doable-if-hackis/68330>\
**Category:** Ruby\
**Created:** [September 11, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/getting-argument-names-and-default-values-doable-if-hackis/68330 "2006-09-11T20:10:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tulkas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5daacb/32.png) [@tulkas](https://www.ruby-forum.com/u/tulkas)\
**Post date:** [September 11, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/getting-argument-names-and-default-values-doable-if-hackis/68330/1 "2006-09-11T20:10:51Z")

</div>

I’ve written some code to get the argument names and their default  
values (if  
any) as methods are defined; it can be found at  
[http://eigenclass.org/hiki.rb?method+arguments+via+introspection](http://eigenclass.org/hiki.rb?method+arguments+via+introspection)

It uses some tricky introspection to do it at runtime. That is, it does  
_not_  
operate like e.g. rdoc, by parsing the source code. Instead, it actually  
#require()s the code, so it will not be confused by your  
meta-programming,  
normally.

I’ve made a simple script that uses that to list the public methods that  
will  
be defined when you require a given .rb file. Its output looks like  
this:

$ method\_args benchmark  
Benchmark#benchmark (caption = “”, label\_width = nil, fmtstr = nil,  
\*labels)  
Benchmark#bm (label\_width = 0, \*labels)  
Benchmark#bmbm (width = 0)  
Benchmark#measure (label = “”)  
Benchmark#realtime ()  
[…]

$ method\_args rational  
Rational.reduce (num, den = 1)  
Rational.new! (num, den = 1)  
Rational#initialize (num, den)  
Rational#+ (a)  
[…]

$ method\_args parsedate  
[…]  
Date#strftime (fmt = “%F”)  
Date#asctime ()  
Date#ctime ()  
DateTime.\_strptime (str, fmt = “%FT%T%Z”)  
DateTime#strftime (fmt = “%FT%T%Z”)  
ParseDate#parsedate (str, comp = false)  
[…]

Something to play with.

---

<div class="post-metadata">

**Author:** ![tulkas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5daacb/32.png) [@tulkas](https://www.ruby-forum.com/u/tulkas)\
**Post date:** [September 11, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/getting-argument-names-and-default-values-doable-if-hackis/68330/2 "2006-09-11T23:53:17Z")

</div>

On Sep 11, 2006, at 11:10 AM, Mauricio F. wrote:

> programming,  
> Benchmark#bm (label\_width = 0, \*labels)  
> Benchmark#bmbm (width = 0)  
> Benchmark#measure (label = “”)  
> Benchmark#realtime ()  
> […]

Hey Mauricio-

```
This is super cool stuff. I'm playing with it now using the version

```

from your site with the patch applied and it spits out all the method  
names but just puts … instead of the arguments. It does this for  
anything i give it from the stdlib or my own code.

ez engine\_yard $ method\_args.rb benchmark  
#\<TypeError: allocator undefined for Binding\>  
Benchmark#benchmark (…)  
Benchmark#bm (…)  
Benchmark#bmbm (…)  
Benchmark#measure (…)  
Benchmark#realtime (…)  
Benchmark::Job#initialize (…)  
Benchmark::Job#item (…)  
Benchmark::Job#report (…)

```
I'm on OSX Tiger latest with my own compiled ruby1.8.4. Does this

```

require 1.8.5 or am I doing something stupid?

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![tulkas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5daacb/32.png) [@tulkas](https://www.ruby-forum.com/u/tulkas)\
**Post date:** [September 12, 2006, 12:43am UTC](https://www.ruby-forum.com/t/getting-argument-names-and-default-values-doable-if-hackis/68330/3 "2006-09-12T00:43:23Z")

</div>

On Tue, Sep 12, 2006 at 06:53:26AM +0900, Ezra Z. wrote:

> This is super cool stuff. I’m playing with it now using the version  
> from your site with the patch applied and it spits out all the method  
> names but just puts … instead of the arguments. It does this for  
> anything i give it from the stdlib or my own code.
> 
> ez engine\_yard $ method\_args.rb benchmark  
> #\<TypeError: allocator undefined for Binding\>  
> Benchmark#benchmark (…)  
> Benchmark#bm (…)  
> […]
> 
> I’m on OSX Tiger latest with my own compiled ruby1.8.4. Does this  
> require 1.8.5 or am I doing something stupid?

The semantics of set\_trace\_func have changed somewhat in 1.8.5 [1], so  
it  
actually requires 1.8.5. I’ll try to ‘backport’ it to 1.8.4 tomorrow.

method\_args will also print (…) for methods defined in C extensions,  
but  
I’ll probably change this to at least show the arity.

[1] it was actually a bug fix; IIRC the binding passed to the trace\_func  
for a  
(c-)return event was the caller’s instead of that of the method returned  
from.  
This is what broke Binding.of\_caller/ruby-breakpoint/Rails’  
breakpointer.  
call\_stack 0.1.0, which I released last week, can overcome that.

---

<div class="post-metadata">

**Author:** ![tulkas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5daacb/32.png) [@tulkas](https://www.ruby-forum.com/u/tulkas)\
**Post date:** [September 12, 2006, 1:07am UTC](https://www.ruby-forum.com/t/getting-argument-names-and-default-values-doable-if-hackis/68330/4 "2006-09-12T01:07:26Z")

</div>

On Tue, Sep 12, 2006 at 06:53:26AM +0900, Ezra Z. wrote:

> ez engine\_yard $ method\_args.rb benchmark  
> #\<TypeError: allocator undefined for Binding\>  
> Benchmark#benchmark (…)  
> Benchmark#bm (…)  
> […]  
> I’m on OSX Tiger latest with my own compiled ruby1.8.4. Does this  
> require 1.8.5 or am I doing something stupid?

Alright, it took only 1 line to make it work on both 1.8.5 and previous  
versions:

$ ruby183 -v method\_args-pre185.rb benchmark  
ruby 1.8.3 (2005-09-21) [i686-linux]  
Benchmark#benchmark (caption = “”, label\_width = nil, fmtstr = nil,  
\*labels)  
Benchmark#bm (label\_width = 0, \*labels)  
Benchmark#bmbm (width = 0)  
Benchmark#measure (label = “”)  
[…]

I’ve updated the code on [eigenclass.org](http://eigenclass.org); could you give it a try?  
(btw. no need to apply the patch in the comments, I had already done so)

---

<div class="post-metadata">

**Author:** ![tulkas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5daacb/32.png) [@tulkas](https://www.ruby-forum.com/u/tulkas)\
**Post date:** [September 12, 2006, 6:27am UTC](https://www.ruby-forum.com/t/getting-argument-names-and-default-values-doable-if-hackis/68330/5 "2006-09-12T06:27:54Z")

</div>

On Sep 11, 2006, at 4:07 PM, Mauricio F. wrote:

> Alright, it took only 1 line to make it work on both 1.8.5 and  
> […]
> 
> I’ve updated the code on [eigenclass.org](http://eigenclass.org); could you give it a try?  
> (btw. no need to apply the patch in the comments, I had already  
> done so)
> 
> –  
> Mauricio F. - [http://eigenclass.org](http://eigenclass.org) - singular Ruby

```
Sweet! It works great now. Thanks Mauricio.

```

ez romper\_room # ruby -v  
ruby 1.8.4 (2005-12-24) [i686-darwin8.5.1]  
ez romper\_room # method\_args.rb benchmark  
#\<TypeError: allocator undefined for Binding\>  
Benchmark#benchmark (caption = “”, label\_width = nil, fmtstr = nil,  
\*labels)  
Benchmark#bm (label\_width = 0, \*labels)  
Benchmark#bmbm (width = 0)  
Benchmark#measure (label = “”)  
Benchmark#realtime ()  
Benchmark::Job#initialize (width)  
Benchmark::Job#item (label = “”)  
Benchmark::Job#report (label = “”)

Cheers-  
-Ezra
