Forum: Ruby Extensions and 'require'

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-02-02 17:09
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?
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-02-02 17:15
Albert Schlef 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?
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-02-02 17:31
Albert Schlef 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.
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 Tim Hunter (timhunter)
on 2009-02-02 18:46
Albert Schlef wrote:
> Albert Schlef 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'.
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-02-03 13:31
Tim Hunter wrote:
>
> In mambo.rb require 'mambo.so'.

But '.so' is specific to unix. Windows, for example, has '.dll'.
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-02-03 13:43
(Received via mailing list)
Albert Schlef wrote:
> Tim Hunter 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.
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-02-03 14:08
Tim Hunter wrote:
> Albert Schlef wrote:
>> Tim Hunter 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.)
This topic is locked and can not be replied to.