Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

How to create a Ruby extension in C in 43 seconds

By Peter Cooper / August 1, 2006

Two months ago, I wrote How to Create a Ruby Extension in C in under 5 minutes. But times have changed!

2012 update: Just tested and this all works in Ruby 1.9.3 almost six years later!

Zenspider, master of all things "Ruby + C", picked up the gauntlet and used RubyInline (previously covered here) to hand my proverbial ass back to me on a platter. All you have to have is gem install RubyInline and.. in just 43 seconds, you too could launch an editor, paste in this code, and check it out:

#!/usr/local/bin/ruby -w

require 'rubygems'
require 'inline'

class Example
  inline(:C) do |builder|
    builder.c "int test1() {
                 int x = 10;
                 return x;
               }"
  end
end

p Example.new.test1

No ruby.h? What ruby.h!? It's as easy as that.

2012 update: I think calling this a "Ruby extension" is a bit of a stretch. But that's six years of hindsight for you ;-)

Other Posts to Enjoy

Twitter Mentions