Installing C + Ruby extensions

I'm using mkmf for building and installing a C extension. I'm also
providing a gem.

I'm thinking of reorganizing the code to extract those C bits that can
be coded in Ruby, and extend them in Ruby, while preserving a C core
that provides a Ruby interface for the libraries I'm linking to.

My problem is how should my source tree look and what tools and how
should I use to have both the Ruby and the C portion built and
installed in the appropriate places.

Preferrably avoiding rubygems, so that I don't force my users to have rubygems.

Which makes me ask: if rubygems will be included in Ruby 2.0, ¿will it
be mandatory?, ¿would it make sense not to make a gem for an
extension?

···

--
Gerardo Santana

Gerardo Santana Gómez Garrido wrote:

I'm using mkmf for building and installing a C extension. I'm also
providing a gem.

I'm thinking of reorganizing the code to extract those C bits that can
be coded in Ruby, and extend them in Ruby, while preserving a C core
that provides a Ruby interface for the libraries I'm linking to.

My problem is how should my source tree look and what tools and how
should I use to have both the Ruby and the C portion built and
installed in the appropriate places.

Preferrably avoiding rubygems, so that I don't force my users to have rubygems.

Which makes me ask: if rubygems will be included in Ruby 2.0, ¿will it
be mandatory?, ¿would it make sense not to make a gem for an
extension?

--
Gerardo Santana

I use Minero Aoki's setup.rb (you can find it on the RAA) for RMagick. The code is a bit opaque but it's robust and mature. The source/build tree that setup uses is here: http://i.loveruby.net/en/projects/setup/doc/devel.html\. RubyGems works well with setup.rb, and it's not hard to build a tar.gz version as well as a gem.

I do not recommend avoiding RubyGems. Unless the typical consumer of your extension is different from the mainstream of Ruby programmers, I don't think it's going to be a problem "forcing" them to have RubyGems. In my experience 99% of Rubyists use RubyGems and probably half of them are not able to install an extension that is not supplied in the form of a gem.

RubyGems is built in to 1.9.0. While I don't think its use will be *mandantory* it seems very useful to supply your extension as a gem. Since the main problem with RubyGems today is that people don't know to require "rubygems" before requiring the gem, having RubyGems built in to Ruby will be very nice indeed, both for us and for our users.

···

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

Thanks Tim for your reply, it has been very helpful.

I have another question.

I have a C core. I'm already extending it in Ruby. There's C code that
could be moved to Ruby. Would I gain anything doing this?

···

--
Gerardo Santana

Gerardo Santana Gómez Garrido wrote:

Thanks Tim for your reply, it has been very helpful.

I have another question.

I have a C core. I'm already extending it in Ruby. There's C code that
could be moved to Ruby. Would I gain anything doing this?

I think so. Ruby code is portable (that is, you know it will run because Ruby is already running) and easier to maintain than C code.

RMagick started life as 100% C but after almost a year of work I threw all the code away. I started over using as much Ruby as possible and never looked back.

···

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html