# File layout best practices for extensions

**URL:** <https://www.ruby-forum.com/t/file-layout-best-practices-for-extensions/79914>\
**Category:** Ruby\
**Created:** [December 20, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/file-layout-best-practices-for-extensions/79914 "2006-12-20T17:45:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bb73d2/32.png) [@Robert\_Au](https://www.ruby-forum.com/u/Robert_Au)\
**Post date:** [December 20, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/file-layout-best-practices-for-extensions/79914/1 "2006-12-20T17:45:31Z")

</div>

I’m implementing a module Foo, with classes Foo::W, Foo::X, Foo::Y, and  
Foo::Z. For various reasons, it is easier to code key parts of Foo::W  
and  
Foo::X in C, but the remainder of the code is easier to write in  
Ruby. Most applications that use this module will need all four  
classes. What’s the best way to lay out the code for use? I assume that  
there should be a top-level Foo.rb that users can require and that  
requires the appropriate .so and .rb files, but is it worth having

require “Foo/W.so”  
require “Foo/X.so”  
require “Foo/W.rb”  
require “Foo/X.rb”  
require “Foo/Y.rb”  
require “Foo/Z.rb”

in Foo.rb? If so, how does one write an extconf.rb to build W.so and  
X.so  
in the same directory? (Or am I forced to keep the sources for those two  
in separate directories, or hand-edit the Makefile?)

---

<div class="post-metadata">

**Author:** ![Robert\_Au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bb73d2/32.png) [@Robert\_Au](https://www.ruby-forum.com/u/Robert_Au)\
**Post date:** [December 20, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/file-layout-best-practices-for-extensions/79914/2 "2006-12-20T19:18:40Z")

</div>

Robert Au wrote:

> require “Foo/W.rb”  
> require “Foo/X.rb”  
> require “Foo/Y.rb”  
> require “Foo/Z.rb”
> 
> in Foo.rb? If so, how does one write an extconf.rb to build W.so and X.so  
> in the same directory? (Or am I forced to keep the sources for those two  
> in separate directories, or hand-edit the Makefile?)

trying to think of a project or tow you might have al ook at for an  
example – maybe ruby-libxml ([http://libxml.rubyforge.org/](http://libxml.rubyforge.org/)).

t.

---

<div class="post-metadata">

**Author:** ![Robert\_Au](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bb73d2/32.png) [@Robert\_Au](https://www.ruby-forum.com/u/Robert_Au)\
**Post date:** [December 20, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/file-layout-best-practices-for-extensions/79914/3 "2006-12-20T19:29:06Z")

</div>

On Dec 19, 2006, at 11:05, Robert Au wrote:

> I’m implementing a module Foo, with classes Foo::W, Foo::X, Foo::Y,  
> and  
> Foo::Z. For various reasons, it is easier to code key parts of  
> Foo::W and  
> Foo::X in C, but the remainder of the code is easier to write in  
> Ruby. Most applications that use this module will need all four  
> classes. What’s the best way to lay out the code for use? I assume  
> that  
> there should be a top-level Foo.rb that users can require and that  
> requires the appropriate .so and .rb files,

If you have a top-level file to require that correctly handles the  
details then it doesn’t matter to me what your internals look like.

> but is it worth having
> 
> require “Foo/W.so”  
> require “Foo/X.so”  
> require “Foo/W.rb”  
> require “Foo/X.rb”  
> require “Foo/Y.rb”  
> require “Foo/Z.rb”
> 
> in Foo.rb?

That is a question only you can answer. You’re going to be  
maintaining it.

> If so, how does one write an extconf.rb to build W.so and X.so  
> in the same directory? (Or am I forced to keep the sources for  
> those two  
> in separate directories, or hand-edit the Makefile?)

I’m not expert, but it seems that mkmf.rb only allows one target per  
create\_makefile. If this is true you’ll need N make files in N  
directories. Don’t hand-edit the Makefile generated by extconf.rb.  
You will only screw it up for platforms that are not your own.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!
