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
31
votes
4 answers

error: failed to build gem native extension when installing rails on mac mountian lion os

I've recently updated to Mountain Lion and re-installed Ruby, but when I try to run a test Rails app, I get an error message that says that "Rails is not currently installed" on my system. I do what it says, type sudo gem install rails and…
Aileen Du
  • 311
  • 1
  • 3
  • 3
19
votes
2 answers

Extending ruby in C - how to specify default argument values to function?

I'm trying to write a C extension to ruby that'll generate a class. I'm looking on how to define some default arguments to a class. For example, if I have this class decleration in ruby: class MyClass def initialize(name, age=10) @name = name …
sa125
  • 28,121
  • 38
  • 111
  • 153
16
votes
2 answers

Ruby C extensions API questions

So, recently I had the unfortunate need to make a C extension for Ruby (because of performance). Since I was having problems with understanding VALUE (and still do), so I looked into the Ruby source and found: typedef unsigned long VALUE; (Link to…
omninonsense
  • 6,644
  • 9
  • 45
  • 66
13
votes
3 answers

Native extensions fallback to pure Ruby if not supported on gem install

I am developing a gem, which is currently pure Ruby, but I have also been developing a faster C variant for one of the features. The feature is usable, but sometimes slow, in pure Ruby. The slowness would only impact some of the potential users…
Neil Slater
  • 26,512
  • 6
  • 76
  • 94
8
votes
1 answer

Data_wrap_struct and mark function

Im writing a Ruby extension and Im using the function Data_wrap_struct. In order to participate in Ruby's mark-and-sweep garbage collection process, I need to define a routine to free my structure, and a routine to mark any references from my…
Pioz
  • 6,051
  • 4
  • 48
  • 67
8
votes
2 answers

mkmf ignores files in sub-folders when it compiles the C extension

I'd like to organize the C source code like this: + / | |___ + ext | | | |___ + native_extension | | | |___ + lib | | | | | |___ (Source files are kept in here - may contain sub-folders) | | | …
Matheus Moreira
  • 17,106
  • 3
  • 68
  • 107
8
votes
1 answer

Ruby Keyword Arguments in C Extensions

How does one handle Ruby 2.0.0 keyword arguments from a C extension? Background def example(name: 'Bob' hat_color: 'red') puts "#{name} has a #{hat_color} hat!" end example #=> "Bob has a red hat!" example(name:…
tydeu
  • 649
  • 5
  • 11
7
votes
2 answers

How to get keys from hash - ruby c extension

I am looking for a function which can get me all the keys from hash or I can loop through the hash to retrieve single key at a time. Currently I am hardcoding key VALUE option = rb_hash_aref(options, rb_str_new2("some_key"));
mandss
  • 91
  • 6
6
votes
1 answer

(U) Ruby Extensions: rb_gc_mark() and instance variables

I'm writing a ruby extension that defines a class. If I use Data_Wrap_Struct() to implement my callback for rb_define_alloc_func(), do I need to manually mark and free the instance variables? Or is that still handled for me?
rampion
  • 87,131
  • 49
  • 199
  • 315
6
votes
1 answer

How do I convert a Block to a Proc in a Ruby 1.9 C extension?

I'm writing a Ruby 1.9 C extension and I want to do the following in ruby: notifier = Notifier.new notifier.on 'click' do puts "clicked!" end Now the problem with this is that on the C method, I only "receive" a block, and, as far as I know, it's…
rubenfonseca
  • 699
  • 10
  • 26
6
votes
1 answer

How I retrieve a 'standalone' symbol on Ruby's C extension API

I want to return several values from a C function, and IMHO, a hash is a good option. I first used rb_intern('A_KEY') to create the keys, but the extension crashed. Now, I am using rb_str_new2, but I prefer symbols. How do I create a new symbol, and…
clbustos
  • 156
  • 6
6
votes
1 answer

Ruby c extensions: How can I catch all exceptions, including things that aren't StandardErrors?

In ruby, begin # ... rescue # ... end won't catch exceptions that aren't subclasses of StandardError. In C, rb_rescue(x, Qnil, y, Qnil); VALUE x(void) { /* ... */ return Qnil; } VALUE y(void) { /* ... */ return Qnil; } will do the same thing.…
Adrian
  • 14,931
  • 9
  • 45
  • 70
6
votes
4 answers

Possible side effects of doing a typedef of a struct to an array of one element in C

I came across this code. typedef __mpz_struct MP_INT; typedef __mpz_struct mpz_t[1]; Here the struct __mpz_struct is a struct that is typedefed to an array of single element. I understand that this is a trick to pass by reference in C. Then mpz_t…
6
votes
3 answers

How do I print values from C extensions?

Every Ruby object is of type VALUE in C. How do I print it in a readable way? Any other tips concerning debugging of Ruby C extensions are welcome.
x-yuri
  • 16,722
  • 15
  • 114
  • 161
6
votes
2 answers

Linking Cocoa headers to ruby C extension

I am working on a C extension for ruby, but I need to include headers from the IOBluetooth framework, specifically: #import #import Everything compiles fine, but at…
WilHall
  • 11,644
  • 6
  • 31
  • 53
1
2 3
11 12