# RDoc, gems, and documentation for dynamically generated constants

**URL:** <https://www.ruby-forum.com/t/rdoc-gems-and-documentation-for-dynamically-generated-constants/167174>\
**Category:** Ruby\
**Created:** [May 5, 2009, 5:15am UTC](https://www.ruby-forum.com/t/rdoc-gems-and-documentation-for-dynamically-generated-constants/167174 "2009-05-05T05:15:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [May 5, 2009, 5:15am UTC](https://www.ruby-forum.com/t/rdoc-gems-and-documentation-for-dynamically-generated-constants/167174/1 "2009-05-05T05:15:36Z")

</div>

Hi,

I’ve got a library that dynamically generates constants the moment  
it’s loaded. It looks something like this at the top of the file:

[‘ALPHA’, ‘BETA’, ‘GAMMA’].each{ |name|  
Dir.const\_set(name, 1)  
}

The problem is that the constants aren’t documented at all when the  
gem is installed. I’d like to be able to document those constants for  
rubygems so that, when the library is installed I can attach a comment  
to each constant.

I tried something like this:

require ‘rdoc/constant’

[‘ALPHA’, ‘BETA’, ‘GAMMA’].each{ |name|  
Dir.const\_set(name, 1)  
RDoc::Constant.new(name, 1, “Dynamically generated constant #  
{name}”)  
}

But that doesn’t work because rubygems doesn’t actually require the  
file, it just parses it, so the RDoc::Constant objects are never  
generated (I think).

What’s the best way to handle this, assuming it’s possible?

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [May 5, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/rdoc-gems-and-documentation-for-dynamically-generated-constants/167174/2 "2009-05-05T21:08:24Z")

</div>

On May 4, 2009, at 20:14, Daniel B. wrote:

> to each constant.
> 
> But that doesn’t work because rubygems doesn’t actually require the  
> file, it just parses it, so the RDoc::Constant objects are never  
> generated (I think).
> 
> What’s the best way to handle this, assuming it’s possible?

I added support for “ghost” methods to the ruby parser in RDoc, the  
same would need to be added to support constants this way.

you can search around the source for GhostMethod to get an idea, and  
I’ll be on IRC later today to help you out (or whenever).

It’s not too difficult a feature to add, there’s tests you can copy,  
too.
