# Distributing native and ruby version of a library

**URL:** <https://www.ruby-forum.com/t/distributing-native-and-ruby-version-of-a-library/64728>\
**Category:** Ruby\
**Created:** [February 8, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/distributing-native-and-ruby-version-of-a-library/64728 "2006-02-08T20:58:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [February 8, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/distributing-native-and-ruby-version-of-a-library/64728/1 "2006-02-08T20:58:31Z")

</div>

Hi,

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?

Patrick

(the directory structure so far)

rb/mylib.rb

c/mylib.rb  
/mylib\_base.c  
/extconf.rb

both should be used by require ‘mylib’ or alike.

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [February 9, 2006, 3:13am UTC](https://www.ruby-forum.com/t/distributing-native-and-ruby-version-of-a-library/64728/2 "2006-02-09T03:13:31Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Patrick G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (the directory structure so far)
> 
> rb/mylib.rb
> 
> c/mylib.rb  
> /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:

begin  
require ‘ext/mylib\_base’  
rescue  
require ‘lib/mylib\_base’  
end

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  
the  
ruby version is used.

Phil

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [February 9, 2006, 3:15am UTC](https://www.ruby-forum.com/t/distributing-native-and-ruby-version-of-a-library/64728/3 "2006-02-09T03:15:19Z")

</div>

Well, last guess before I head to bed:

In “mylib.rb”:

begin  
require “mylib\_impl\_c” # Attempts to load the C shared library.  
rescue LoadError  
require “mylib\_impl\_rb” # Falls back to the ruby implementation.  
end

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

David V.

DÅ?a Streda 08 FebruÃ¡r 2006 20:58 Patrick G. napÃ­sal:

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [February 9, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/distributing-native-and-ruby-version-of-a-library/64728/4 "2006-02-09T14:43:38Z")

</div>

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 ([http://raa.ruby-lang.org/project/setup/3.2.4](http://raa.ruby-lang.org/project/setup/3.2.4)

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.

Patrick
