Forum: Ruby distributing native and ruby version of a library

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ef5256760771d0c5cad01dc3b35382ba?d=identicon&s=25 Patrick Gundlach (Guest)
on 2006-02-08 20:58
(Received via mailing list)

I have a simple library, one in a pure ruby version and one in a
ruby/c version (which is quicker). I would like to distribute both and
let the user decide (by checking whether a c library is available)
which version to install. What would a clever way to accomplish that?
Should I make the decision when running extconf.rb? Any hints?


(the directory structure so far)



both should be used by require 'mylib' or alike.
Eb9493c94d8db9887e5f15284d2c767f?d=identicon&s=25 unknown (Guest)
on 2006-02-09 03:13
(Received via mailing list)
In article <>,
Patrick Gundlach  <> wrote:
>(the directory structure so far)
> /mylib_base.c
> /extconf.rb
>both should be used by require 'mylib' or alike.

How about in your rb/mylib.rb file you do something like:

    require 'ext/mylib_base'
    require 'lib/mylib_base'

Where ext/mylib_base would be the shared lib created from the C file and
lib/mylib_base would be the Ruby implementation.

The user of mylib just does:

  require 'mylib'

....and it's all transparent to the user whether or not the C version or
ruby version is used.

430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-09 03:15
(Received via mailing list)
Well, last guess before I head to bed:

In "mylib.rb":

	require "mylib_impl_c" # Attempts to load the C shared library.
rescue LoadError
	require "mylib_impl_rb" # Falls back to the ruby implementation.

Yay for gracefully degrading functionality. Or performance, for that
Sprinkle with configuration parameters to let user determine at runtime
backend to use to taste.

David Vallner

DÅ?a Streda 08 Február 2006 20:58 Patrick Gundlach napísal:
Ef5256760771d0c5cad01dc3b35382ba?d=identicon&s=25 Patrick Gundlach (Guest)
on 2006-02-09 14:43
(Received via mailing list)
Hi Phil (and David),


> The user of mylib just does:
>   require 'mylib'
> ...and it's all transparent to the user whether or not the C version or the
> ruby version is used.

OK, that makes sense. Thanks. I wonder if this could be combined with
the setup.rb from RAA (<>

When I run

ruby setup.rb config

setup.rb tries to create the c-extension in ext/mylib/... but this
might fail if the user doesn't have the necessary libraries installed.
Is there a way in setup.rb to say 'keep going if you can't create the
c extension'? I mean as a configuration file or alike, not as a
parameter passed to setup.rb. This way the c-extension will installed
only if available.

This topic is locked and can not be replied to.