Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > RDoc vs. alias_method

Reply
Thread Tools

RDoc vs. alias_method

 
 
Noah Easterly
Guest
Posts: n/a
 
      08-10-2007
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?

 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      08-11-2007
On Aug 10, 2007, at 08:16, Noah Easterly wrote:
> 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?


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.

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



 
Reply With Quote
 
 
 
 
Daniel DeLorme
Guest
Posts: n/a
 
      08-11-2007
Noah Easterly wrote:
> 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?


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

 
Reply With Quote
 
Noah Easterly
Guest
Posts: n/a
 
      08-13-2007
On Aug 10, 9:14 pm, Eric Hodel <(E-Mail Removed)> wrote:
> On Aug 10, 2007, at 08:16, Noah Easterly wrote:
>
> > 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?

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use rdoc parsers outside of rdoc? Paul Van Delst Ruby 0 07-27-2006 07:18 PM
rdoc: how to add readme.rdoc as index.html? Iwan van der Kleyn Ruby 1 04-26-2005 12:37 PM
[RDOC] Using a template causes rdoc not to document some classes Daniel Berger Ruby 1 11-02-2004 08:23 PM
rdoc bug (and rdoc bug tracker site is down) Brian Schröder Ruby 5 09-18-2004 02:08 PM
rdoc: how to generate rdoc & ri documentation of standard library? Andreas Schwarz Ruby 6 01-01-2004 03:09 AM



Advertisments