# RDoc vs. alias\_method

**URL:** <https://www.ruby-forum.com/t/rdoc-vs-alias-method/111769>\
**Category:** Ruby\
**Created:** [August 10, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/rdoc-vs-alias-method/111769 "2007-08-10T17:17:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noah\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ba0ec/32.png) [@Noah\_E](https://www.ruby-forum.com/u/Noah_E)\
**Post date:** [August 10, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/rdoc-vs-alias-method/111769/1 "2007-08-10T17:17:57Z")

</div>

I’m working on a script that has to work across several versions of  
ruby.  
Sometimes, some versions don’t have libraries that I require, so I get  
around that by  
having an alternate implementation for those versions.

For easy testing I like to be able to declare all the implementations  
so I can run them in parallel, and compare results, and create aliases  
to the ‘best’ implementation.

For example, if in 1.8.8, dentist.rb is available, and before that,  
you could only get hygenist.rb, I might do something like this:

| class ToothCare  
| dentist\_load\_error = nil  
| begin  
| require ‘dentist’  
| # gargle!  
| def dentist\_gargle  
| #…  
| end  
| # rinse!  
| def dentist\_rinse  
| #…  
| end  
| # spit!  
| def dentist\_spit  
| #…  
| end  
| rescue LoadError =\> dentist\_load\_error  
| end  
| hygenist\_load\_error = nil  
| begin  
| require ‘hygenist’  
| # gargle!  
| def hygenist\_gargle  
| #…  
| end  
| # rinse!  
| def hygenist\_rinse  
| #…  
| end  
| # spit!  
| def hygenist\_spit  
| #…  
| end  
| rescue LoadError =\> hygenist\_load\_error  
| end  
| prefix = if dentist\_load\_error.nil?  
| ‘dentist’  
| elsif hygenist\_load\_error.nil?  
| ‘hygenist’  
| else  
| raise LoadError, [  
| “Could not find dentist or hygenist, sorry…”,  
| dentist\_load\_error,  
| hygenist\_load\_error  
| ].join("\n")  
| instance\_methods.each do |method|  
| next unless method =~ /^#{prefix}\_(\w+)$/  
| alias\_method $1, method  
| end  
| end

Whoever uses ToothCare can just call #gargle,#rinse,or #spit - they  
don’t have to worry about which implementation they’re using.  
Meanwhile, I can use #dentist\_gargle and #hygenist\_gargle to test and  
compare.

This works pretty well, except when I run RDoc. It’s not smart enough  
to figure out that the alias\_method creates instance methods named  
#gargle,#rinse, and #spit, and so they don’t get documented.

Is there any easy and DRY way I can tell RDoc to document those  
methods?

---

<div class="post-metadata">

**Author:** ![Noah\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ba0ec/32.png) [@Noah\_E](https://www.ruby-forum.com/u/Noah_E)\
**Post date:** [August 11, 2007, 3:14am UTC](https://www.ruby-forum.com/t/rdoc-vs-alias-method/111769/2 "2007-08-11T03:14:53Z")

</div>

On Aug 10, 2007, at 08:16, Noah E. wrote:

> methods?  
> Nope. The Ruby parser doesn’t understand the document-method stuff  
> that the C parser has.

… So writing a patch and submitting it on the RDoc bug tracker is  
the most DRY way to do that.

---

<div class="post-metadata">

**Author:** ![Noah\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ba0ec/32.png) [@Noah\_E](https://www.ruby-forum.com/u/Noah_E)\
**Post date:** [August 11, 2007, 3:32am UTC](https://www.ruby-forum.com/t/rdoc-vs-alias-method/111769/3 "2007-08-11T03:32:54Z")

</div>

Noah E. wrote:

> For example, if in 1.8.8, dentist.rb is available, and before that,  
> | # rinse!  
> | begin  
> | def hygenist\_spit  
> | “Could not find dentist or hygenist, sorry…”,  
> don’t have to worry about which implementation they’re using.  
> Meanwhile, I can use #dentist\_gargle and #hygenist\_gargle to test and  
> compare.
> 
> This works pretty well, except when I run RDoc. It’s not smart enough  
> to figure out that the alias\_method creates instance methods named  
> #gargle,#rinse, and #spit, and so they don’t get documented.
> 
> Is there any easy and DRY way I can tell RDoc to document those  
> methods?

hmm, maybe you can split your code into mixins;

begin  
require ‘dentist’  
module DentistToothCare  
def dentist\_gargle  
#…  
end  
…  
instance\_methods.each do |method|  
alias\_method method, “dentist\_#{method}”  
end  
end  
rescue LoadError =\> dentist\_load\_error  
end

class ToothCare  
if dentist\_load\_error.nil?  
include DentistToothCare  
else  
include HygenistToothCare  
end  
#doc for “gargle”  
def gargle; super; end  
end

Just a (dumb?) idea

Daniel

---

<div class="post-metadata">

**Author:** ![Noah\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7ba0ec/32.png) [@Noah\_E](https://www.ruby-forum.com/u/Noah_E)\
**Post date:** [August 13, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/rdoc-vs-alias-method/111769/4 "2007-08-13T14:50:58Z")

</div>

On Aug 10, 9:14 pm, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Poor workers blame their tools. Good workers build better tools. The  
> best workers get their tools to do the work for them. – Syndicate Wars

Thanks, all. As an immediate solution, I decided to make  
DentistToothCare and HygenistToothCare sub-classes of ToothCare, and  
overwrote ToothCare::new to return  
one or the other, depending.

And if I get the time, I’ll go for the longer term solution of  
patching RDoc, since I use dynamic aliasing and code generation often  
enough to make this an issue.
