# Extensions and 'require'

**URL:** <https://www.ruby-forum.com/t/extensions-and-require/159232>\
**Category:** Ruby\
**Created:** [February 2, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/extensions-and-require/159232 "2009-02-02T17:09:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [February 2, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/extensions-and-require/159232/1 "2009-02-02T17:09:54Z")

</div>

Hello!

I’m creating a ‘gem’ package for a ruby extension written in C.

I have a little problem:

First, let’s suppose the package is called “Mambo”.

Now, the “.so” (or “.dll”, or whatever) is called “mambo.so”.

In addition to “mambo.so”, I also have a “mambo.rb” which adds some  
finishing touches to the Mambo module defined in “mambo.so”.

The problem is this:

If the user does “require ‘mambo’” in his code, Ruby will load  
“mambo.rb”. It won’t load “mambo.so” (because require first checks for  
.rb, only then for .so).

So how can I cause “mambo.so” to get loaded?

One solution is to rename “mambo.rb” to, say, “mambo\_something.rb”, and  
in it to “require ‘mambo’”. But… it doesn’t look neat.

What’s the “official” way to solve this problem?

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [February 2, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/extensions-and-require/159232/2 "2009-02-02T17:15:03Z")

</div>

Albert S. wrote:

> […]  
> One solution is to rename “mambo.rb” to, say, “mambo\_something.rb”, and  
> in it to “require ‘mambo’”. But… it doesn’t look neat.

Ah, another solution, I think, is to have “autorequires =  
[‘mambo\_something’, ‘mambo’]” in the gem spec. This will load the two  
files. Still, it doens’t look neat to have such a central file,  
‘mambo\_something.rb’, have something tucked to its name. Or does it?

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [February 2, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/extensions-and-require/159232/3 "2009-02-02T17:31:48Z")

</div>

Albert S. wrote:

> […]  
> So how can I cause “mambo.so” to get loaded?

Hello again.

I found two gems, “rubynode” and “bcrypt” that solve this problem by  
appending “\_ext” to the .so name.

So it seems that I’ll have “mambo.rb” and “mambo\_ext.so”. The former  
will ‘require’ the latter.

I’m still interested in hearing other opinions.

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [February 2, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/extensions-and-require/159232/4 "2009-02-02T18:46:23Z")

</div>

Albert S. wrote:

> Albert S. wrote:
> 
> > […]  
> > So how can I cause “mambo.so” to get loaded?
> 
> Hello again.
> 
> I found two gems, “rubynode” and “bcrypt” that solve this problem by  
> appending “\_ext” to the .so name.
> 
> So it seems that I’ll have “mambo.rb” and “mambo\_ext.so”. The former  
> will ‘require’ the latter.
> 
> I’m still interested in hearing other opinions.

In mambo.rb require ‘mambo.so’.

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [February 3, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/extensions-and-require/159232/5 "2009-02-03T13:31:06Z")

</div>

Tim H. wrote:

> In mambo.rb require ‘mambo.so’.

But ‘.so’ is specific to unix. Windows, for example, has ‘.dll’.

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [February 3, 2009, 2:08pm UTC](https://www.ruby-forum.com/t/extensions-and-require/159232/6 "2009-02-03T14:08:58Z")

</div>

Tim H. wrote:

> Albert S. wrote:
> 
> > Tim H. wrote:
> > 
> > > In mambo.rb require ‘mambo.so’.
> > 
> > But ‘.so’ is specific to unix. Windows, for example, has ‘.dll’.
> 
> And OS X has .dylib. Ruby translates .so to whatever is appropriate for  
> the host.

Ah. That’s nice. Thanks.

(I’ve found two more gems that use the ‘\_ext’ pattern.)

---

<div class="post-metadata">

**Author:** ![alby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85f322/32.png) [@alby](https://www.ruby-forum.com/u/alby)\
**Post date:** [February 3, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/extensions-and-require/159232/7 "2009-02-03T13:43:18Z")

</div>

Albert S. wrote:

> Tim H. wrote:
> 
> > In mambo.rb require ‘mambo.so’.
> 
> But ‘.so’ is specific to unix. Windows, for example, has ‘.dll’.

And OS X has .dylib. Ruby translates .so to whatever is appropriate for  
the host.
