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

How should marking be accomplished on arrays of VALUE* in a Ruby extension?

I have a matrix type which contains a void* array, representing an array of objects (which are all of one type in a given matrix, e.g., all C integers, all floats, doubles, a variety of structs, or possibly even all Ruby VALUEs). Memory allocation…
Translunar
  • 3,739
  • 33
  • 55
2
votes
1 answer

Using rb_require with rb_protect to embed Ruby in C

I want to use rb_require with rb_protect, as in the following example: int error; rb_protect( (VALUE (*)(VALUE))rb_require, (VALUE) "./test", &error); But when I compile it, I get this error: passing argument 1 of ‘rb_protect’ from incompatible…
cedlemo
  • 3,205
  • 3
  • 32
  • 50
2
votes
2 answers

How to control output generated from extconf.rb depending on OS?

I'm writing a Ruby C Extension. I will be compiling it under Windows and OSX. What I have been unable to work out is control where the makefile and all the rest of the generated files are placed. My extconf.rb file got conditional statements for…
thomthom
  • 2,854
  • 1
  • 23
  • 53
2
votes
1 answer

Strange behaviour with costructors in Ruby C extension

I have see strange behaviour with class costructors in Ruby C extension. See an example: we have a class Foo that is a C extension and a class Bar that inherits from Foo: extconf.rb # extconf.rb require 'mkmf' create_makefile('foo/foo') foo.c //…
Pioz
  • 6,051
  • 4
  • 48
  • 67
2
votes
1 answer

How to run my ruby application using ruby extension library (in c), in windows?

I would be thankful for any help on this: I want to write an extension to my c library. I created file zmq.cpp that uses library libzmq (written in C++). I created makefile using ruby extconf.rb, then I run nmake. It all went fine. Nmake generated…
Tamara Kustarova
2
votes
1 answer

Ruby and C API: rb_funcall_with_block on global method? How to call ruby global method with block from C?

I want to call a global method in ruby from my C API code. So in ruby: def giveMeABlock(*args) puts "Starting giveMeABlock with #{args.inspect}" yield if block_given? end As I've since learned, global functions are actually just private…
2
votes
1 answer

What is the best practice when type checking option hash values in a ruby C extension?

I'm developing a C extension for ruby, one of the functions from the C library I'm accessing receives an options struct which seems to be naturally translate to an options hash in ruby-world. The struct is being initialized with known default…
Camilo
  • 635
  • 1
  • 5
  • 9
2
votes
0 answers

Document methods in a ruby c extension

I've been working on some ruby libraries with C extensions now. And I want to give users access to the irb#help method, e.g: > help("Array#empty?") # Prints documentation for the method. I've been using the same way to name functions as ruby core,…
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
2
votes
1 answer

What am I supposed to return as class in Data_Wrap_Struct?

This is my stripped down code which isolates my problem: #include "ruby.h" #include "stdlib.h" typedef struct HandValues { double pair1; double pair2; } HandValues; static VALUE get_pairs_2(self) VALUE self; { HandValues *MadeHand…
Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114
2
votes
2 answers

How do I access a ruby array from my c extension?

I'm getting this error ev.c:11: error: subscripted value is neither array nor pointer for this line printf("%d\n", pairs[0][0]); In this code static VALUE EV; static VALUE PairCounter; static VALUE sort_pairs_2(VALUE self) { VALUE pairs; …
Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114
2
votes
3 answers

How do I extend my ruby class with a c extension?

If I have Foo::Bar written in Ruby, and I want to add a method to Bar as a C extension. Right now when I create Foo::Bar in C like this: static VALUE Foo; static VALUE Bar; static VALUE print_string(VALUE self, VALUE string) { printf("%s",…
Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114
2
votes
0 answers

Stack level too deep error in ruby native extension, how to reduce stack level?

I have a Ruby on Rails api which handles a simple API call and returns some encrypted data. The encryption is done in C++, using the ruby native C api. (reference here). The native part works fine when compiled and linked as a standalone program,…
Vincent
  • 185
  • 1
  • 11
2
votes
1 answer

Is memory freed after using StringValueCStr?

I use StringValueCStr to convert ruby String to char*. Do i need to free memory when this new C string is no longer needed? If this new C string is freed automatically, does this mean i should copy it if i plan on saving it in my C structures for…
user124
  • 1,632
  • 1
  • 11
  • 11
2
votes
3 answers

ruby native extension: undefined symbol

I'm attempting to create a ruby native extension, but when I run rake which uses ext/example_project/extconf.rb to build my project and run my tests under test/, I get the following error when the tests are…
JesseBuesking
  • 6,496
  • 4
  • 44
  • 89
2
votes
1 answer

How to pass keywords to a Ruby method from a C extension?

I'm working on a C extension for Ruby and I want to call a method which has required keyword arguments, like this: class Word def initialize(line:, col:, value:) end end In C, I'm familiar, with calling Ruby methods via rb_funcall and…
rmosolgo
  • 1,854
  • 1
  • 18
  • 23