Extensions and 'require'

A

Albert Schlef

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?
 
A

Albert Schlef

Albert said:
[...]
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?
 
A

Albert Schlef

Albert said:
[...]
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.
 
T

Tim Hunter

Albert said:
Albert said:
[...]
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'.
 
T

Tim Hunter

Albert said:
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.
 
A

Albert Schlef

Tim said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

require problem 0
require - not loading gem 2
Circular Require Warning by gsl and curb 0
require problem 3
circular 'require' 28
Confused with "include" and require 2
require wackyness 2
trying to require nokogiri 2

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top