Question

I have Rails 4 application. I created simple C extension with Ruby C API This extension convert file from data folder and returns output based on that file(some_file.f)

|-- data
|   |--some_file.f
|-- ext
|   `-- my_ext
|       |-- extconf.rb
|       |-- some_lib.c
|       `-- my_ext.c
|-- lib

Right now when I want to use that extension I need to run following commands in my_ext dir

ruby extconf.rb
make

Then I have additional .o .bundle complied files. I also created initialiser with require 'my_ext/my_ext'

  1. Where should I move these files? Into lib/my_ext?

  2. Is there a way to improve that compile process and better organise complied files? In future I would like to add also tests and run them after extension compiled. Maybe I should create a gem from that extension? Or rake task?

  3. I read http://tenderlovemaking.com/2009/12/18/writing-ruby-c-extensions-part-1.html this tutorial but how to implement that in Rails application? Where to put Rakefile?

Was it helpful?

Solution

I would just look into packaging your extension as a standalone rubygem. Then you can add it to your Gemfile and just let rails handle everything. There's a bit more to creating a gem with an extension than a gem with just ruby, but this guide goes over it well. If you'd like to keep the gem private you should check out this tutorial. You'll also have to modify your Gemfile to search for metadata on your server in addition to rubygems.org, or wherever you currently look.

If you start trying to manually manage the extension yourself you can get into quite a few gotcha situations, such as needing a ruby build with the configuration option --enable-shared set (for allowing you to link to the shared object with a basic require statement) or having to restart the entire rails app to see any changes in the lib/ directory (assuming that's where you'd manually put the extension). I've gotten bitten by those two in the past and since then have switched to creating private gems for this type of stuff because the gem installation process is so easy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top