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
4
votes
1 answer

Data_Wrap_Struct and destruction order

I'm writing an Ruby extension for a physics engine. This physics engine has bodies that are linked to a world, so my Ruby objects are World and Body. A body is constructed (in C++) with world->CreateBody and destroyed with world->DestroyBody. The…
André Wagner
  • 1,330
  • 15
  • 26
4
votes
1 answer

Ruby C API string and symbol equality?

Writing a C extension for a Ruby gem, I need to test a parameter value for equality with a known symbol and string. I realize you can intern a string with char *foo = "foo"; VALUE foo_string_value = rb_intern(foo); and then convert it into a…
Gene
  • 46,253
  • 4
  • 58
  • 96
4
votes
1 answer

How to write non-static method in C++ class in Ruby-C++ extension?

I'm developing a Ruby-C++ extension. I have to write a non-static method in a CPP class and I have to invoke that class method in ruby client by using the class instance. Following is the main.cpp: #include "ruby.h" #include using…
BSalunke
  • 11,499
  • 8
  • 34
  • 68
4
votes
1 answer

Add a C native method to a pre-existing Ruby class

I would like to know how to add a native method written in a C extension to a pre-existing Ruby class ? I only found function that allow you to create new Ruby class, but none which returns a pre-existing class.
yageek
  • 4,115
  • 3
  • 30
  • 48
4
votes
1 answer

Compiling Ruby C++ Extension

I am trying to compile a c++ extension for Ruby, and the compilation does not return an error, but it does not seem to be compiling correctly. What am I doing wrong? I have the main cpp script foo.cpp: #include #include extern…
sawa
  • 165,429
  • 45
  • 277
  • 381
3
votes
3 answers

How can I bind a C/C++ structure to Ruby?

I need some advice how can I bind a C/C++ structure to Ruby. I've read some manuals and I found out how to bind class methods to a class, but I still don't understand how to bind structure fields and make them accessible in Ruby. Here is the code…
l1nx
  • 31
  • 2
3
votes
2 answers

Accepting an undefined number of arguments in Ruby/Inline C

I am trying to rewrite a highly recursive function using inline C with Ruby. The function accepts an undefined number of arguments, i.e. it would look like this in Ruby: def each_entity(*types) # Do something and recurse. end I am…
user2398029
  • 6,699
  • 8
  • 48
  • 80
3
votes
1 answer

Ruby 1.8.7: Segfault on calling function inside C extension

I'm building a simple C extension for a Ruby module, and I'm running into trouble with a segfault when I call another C function inside my extension. The basic flow of execution goes like this: I create a Ruby class and call an instance method on…
Tim
  • 59,527
  • 19
  • 156
  • 165
3
votes
1 answer

Difference between Data_Wrap_Struct and TypedData_Wrap_Struct?

I'm wrapping a C struct in a Ruby C extension but I can't find the differente between Data_Wrap_Struct and TypedData_Wrap_Struct in the docs, what's the difference between the two functions?
user5062790
3
votes
1 answer

Requiring lib (openmp) in ruby C extension gem [OSX]

I am starting to develop ruby gem extensions in C and I am stuck with a problem that appears to be simple. What I am trying to do is to create a simple gem extension that will execute some code in C that uses openmp. Here are my extconf.rb and my c…
Raphael Ottoni
  • 506
  • 3
  • 14
3
votes
1 answer

Yard and C extension

I have a C ruby extension that I document with rdoc. There are both C files and ruby files that are parsed by rdoc. Does yard can do the same and is there an "easy way", (I mean a commonly used way) to migrate from rdoc to yard?
cedlemo
  • 3,205
  • 3
  • 32
  • 50
3
votes
1 answer

ruby c extension how to manage garbage collection between 2 objects

I have a C extension in which I have a main class (class A for example) created with the classical: Data_Wrap_Struct rb_define_alloc_func rb_define_private_method(mymodule, "initialize" ...) This A class have an instance method that generate B…
cedlemo
  • 3,205
  • 3
  • 32
  • 50
3
votes
2 answers

Ruby C extension: Is there a way to finalize?

I have been through all the documents on Ruby C extensions that I can find to no good end. Is there a complement to the Init_... method of initializing a C extension that is called as the interpreter exits?
Gene
  • 46,253
  • 4
  • 58
  • 96
3
votes
1 answer

Ruby C Extensions, loading external libraries

I'm trying to build a Ruby C Extension for Raspberry Pi using some existing C code. The code relies on the bcm2835-1.35 library for some functions. Problem is that when I try to run the a ruby program using on the RPi, I get the following: ruby:…
user160917
  • 9,211
  • 4
  • 53
  • 63
3
votes
1 answer

How do I hide instance variables from the Ruby layer in my C-extension?

I am looking into example from Programming Ruby 1.9. Is it possible to create instance variables not exposed to Ruby, visible only in C - for example to initialize C-structure in t_init and use it in t_add ? If I declare the structure or variable…
bsrdjan
  • 436
  • 3
  • 10
1 2
3
11 12