The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Writing C Extensions, Improved

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ryan Davis

Posts: 651
Nickname: zenspider
Registered: Oct, 2004

Ryan Davis is a ruby nerd.
Writing C Extensions, Improved Posted: Aug 1, 2006 3:20 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Ryan Davis.
Original Post: Writing C Extensions, Improved
Feed Title: Polishing Ruby
Feed URL: http://blog.zenspider.com/index.rdf
Feed Description: Musings on Ruby and the Ruby Community...
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Ryan Davis
Latest Posts From Polishing Ruby

Advertisement

I recently read How to create a Ruby extension in C in under 5 minutes by Peter Cooper. I decided to replicate the example to see how things fare these days. I basically followed his lead, typing in everything, but cleaning up the code to suit my tastes a bit better (and in some cases, to be more correct in C-land).

First, I wrote the following:

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

require 'mkmf'
extension_name = 'example'
dir_config(extension_name)
create_makefile(extension_name)

Nothing fancy, just a simple mkmf generator. I don't like mkmf (esp. its insides), but this is clean enough. After that, I wrote (and cleaned up) the C file:

#include "ruby.h"

static VALUE method_test1(VALUE self) {
  int x = 10;
  return INT2NUM(x);
}

void Init_example() {
  VALUE Example = rb_define_module("Example");
  rb_define_method(Example, "test1", method_test1, 0);
}

Nothing fancy. The only real differences from the original were made to keep the symbol table clean. All in all, it took me about 7 minutes, not too far off from the original article's title. And sure enough, it can be run just fine via:

./example-build.rb
make
ruby -I. -rexample -e 'include Example; puts test1'

Not too bad. I don't have any real complaints about that... except that the alternative is so much easier and so much cleaner that I don't really see why anyone would choose this way of going about it. Examine:

#!/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

and you run it with:

./example.rb

Notice how I just run the script? That is because compiling, linking, and loading are all handled for you, as needed, and you can never accidentally skip a step. Notice how I just write plain C? int test1() is about as clean as you can get. Notice how I return x straight up? No type conversions here.

The point is to remove the distractions so you can get your work done. No more wondering "did I type make?" or "what is the proper macro to convert a string?" as it is all handled for you. Give developers the tools they need so they can stay in thought, not in process.

It doesn't get simpler than this... and really, it shouldn't ever be more difficult than this.

Read: Writing C Extensions, Improved

Topic: I made Digg! Previous Topic   Next Topic Topic: Typo 4 Teething Pains

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use