Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > distributing native and ruby version of a library

Reply
Thread Tools

distributing native and ruby version of a library

 
 
Patrick Gundlach
Guest
Posts: n/a
 
      02-08-2006
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.

 
Reply With Quote
 
 
 
 
Phil Tomson
Guest
Posts: n/a
 
      02-09-2006
In article <(E-Mail Removed)>,
Patrick Gundlach <(E-Mail Removed)> wrote:
>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.
>



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
 
Reply With Quote
 
 
 
 
David Vallner
Guest
Posts: n/a
 
      02-09-2006
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=
=2E=20
Sprinkle with configuration parameters to let user determine at runtime whi=
ch=20
backend to use to taste.

David Vallner

D=C5=88a Streda 08 Febru=C3=A1r 2006 20:58 Patrick Gundlach nap=C3=ADsal:
> 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.



 
Reply With Quote
 
Patrick Gundlach
Guest
Posts: n/a
 
      02-09-2006
Hi Phil (and David),


[...]

> 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.



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>

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ruby version 1.9.2 dev with memcached-0.1.18 native compilationerror/syntax errors Andrew Milkowski Ruby 0 02-23-2010 01:12 AM
ruby version 1.9.2 dev with memcached-0.1.18 native compilationerror/syntax errors Andrew Milkowski Ruby 0 02-23-2010 12:44 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? MowGreen [MVP] ASP .Net 5 02-09-2008 01:55 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 AM



Advertisments