Questions tagged [ruby-c-extension]

Loadable modules written in C which provide additional functionality for the Ruby language.

C extensions are shared objects that can be loaded by the MRI at runtime. They are typically created in order to allow Ruby to interface with low-level code, but can also used to improve the speed of expensive computations.

Many modules in the Ruby standard library are implemented as C extensions in order to optimize for speed or reuse existing libraries.

176 questions
6
votes
1 answer

unable to create a C++ ruby extension

I have problems creating a ruby extension to export a C++ library I wrote to ruby under OSX. This simple example: #include extern "C" void Init_bayeux() { boost::regex…
Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35
6
votes
1 answer

how to rb_protect everything in ruby

I want to call ruby code from my own C code. In case an exception gets raised, I have to rb_protect the ruby code I call. rb_protect looks like this: VALUE rb_protect(VALUE (* proc) (VALUE), VALUE data, int * state) So proc has to be a function…
johannes
  • 7,262
  • 5
  • 38
  • 57
6
votes
1 answer

"resources"-directory for ruby gem

I'm currently experimenting with creating my own gem in Ruby. The gem requires some static resources (say an icon in ICO format). Where do I put such resources within my gem directory tree and how to I access them from code? Also, parts of my…
DeX3
  • 5,200
  • 6
  • 44
  • 68
5
votes
2 answers

When developing a rubygem with C extensions, how do you test locally with Rspec?

I'm writing a gem, that includes a C extension. Usually when I write a gem, I follow a process of TDD, where I'll write a failing spec and then work on the code until it passes, etc etc... With my C extension in "ext/mygem/mygem.c" and a valid…
d11wtq
  • 34,788
  • 19
  • 120
  • 195
5
votes
1 answer

How do I avoid cross-thread violations in a Ruby extension?

I'm writing a C extension, providing an interface between Ruby and an asynchronous I/O library. When running the tests over my code, I frequently get errors including (but not limited to): [BUG] cross-thread violation in…
Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78
4
votes
2 answers

Using ruby's BigDecimal in the C API

In order to create a BigDecimal from a C string in a Ruby extension, I'm doing this: rb_funcall(rb_path2class("BigDecimal"), rb_intern("new"), 1, rb_str_new("0.0777", 6)); // => BigDecimal.new("0.0777") Is there a shorter way to do this? Also,…
d11wtq
  • 34,788
  • 19
  • 120
  • 195
4
votes
3 answers

How do I use other build systems with rubygems?

My C code is getting harder to manage due to the inflexibility of mkmf. For this reason, I'd like to use another build system. What does rubygems need in order to build a C extension? How can I integrate a build system like autotools/configure into…
Matheus Moreira
  • 17,106
  • 3
  • 68
  • 107
4
votes
2 answers

Defining classes in modules with the Ruby C API

I am trying to define a class inside a module with the Ruby C API. However, the way I have seen this done all over the net doesn't seem to work for me. Specifically, the top-level module gets created but the class can't be found inside the module.…
user2398029
  • 6,699
  • 8
  • 48
  • 80
4
votes
2 answers

Why is RARRAY_LEN not being allocated?

I'm using the C extension methods to create a new ruby array, but RARRAY_LEN is not getting set. Am I doing something wrong? long int max = 4; VALUE rAry; rAry = rb_ary_new2(max); printf("allocated: %lu\n", RARRAY_LEN(rAry)); output: allocated:…
Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114
4
votes
1 answer

Native C Extension if Library is Available

I'm building a native C extension Ruby gem for generating unique identifiers (found here). I'd like the library to use libuuid if possible (through C extensions) and fall back to a simple Ruby implementation. I currently have both the C and Ruby…
Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232
4
votes
2 answers

Why are the values I am pulling from my ruby array to my c extension wrong?

This method is just verifying that I'm able to see the elements of a ruby array correctly. static VALUE print_cards(self) VALUE self; { VALUE cards; int i; cards = rb_ivar_get(self, rb_intern("@cards")); VALUE *ary_ptr =…
Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114
4
votes
2 answers

Ruby extension wrapper around C library fails to load installed library

The Ruby extension davenport-ruby to the C library davenport will not load properly on Ubuntu and Debian. It works alright on a development machine (MacOS), as demonstrated by the README of the smoke test Ruby project, dvt The RubyGems loader (via…
4
votes
1 answer

How can a Ruby C extension store a proc for later execution?

Goal: allow c extension to receive block/proc for delayed execution while retaining current execution context. I have a method in c (exposed to ruby) that accepts a callback (via VALUE hash argument) or a block. // For brevity, lets assume m_CBYO is…
codenamev
  • 2,203
  • 18
  • 24
4
votes
2 answers

Passing ruby array values into a C array

I'm trying to make a standalone FFT extension for ruby in C, based on this recipe I've noted several methods for passing different values between ruby and c. However im fairly new to both ruby and C and can't work out how to copy an array from a…
Nat
  • 2,689
  • 2
  • 29
  • 35
4
votes
1 answer

Ruby Data_Get_Struct error wrong argument expect Data

I am writting a little ruby module with some very simple classes in C: typedef struct window_t { GtkWidget * widget; } static void c_window_struct_free(window_t *c) { if(c) { ruby_xfree(c); } } static VALUE c_window_struct_alloc( VALUE…
cedlemo
  • 3,205
  • 3
  • 32
  • 50
1
2
3
11 12