Using FFI or Fiddle on an Embedded Ruby

I would like to be able to pass Ruby varargs as an argument to a C
function created with rb_define_method. The typical prototype for a C
function with argc = -1 is VALUE func(int argc, VALUE *argv, VALUE obj).

The following is a code sample that would be representative of the
situation but yet would work with a standard Ruby.


module EmbeddedRuby
extend Fiddle::Importer

typealias “VALUE”, “unsigned long”

extern “VALUE rb_ary_cat(VALUE, const VALUE *, long)”

a = [ “a”, “b”, “c” ]
b = [“d”, “e”, “f”]

puts a.dup.push(b).inspect
puts a.dup.push(*b).inspect

EmbeddedRuby.rb_ary_cat( << 1), << 1).ref,

The Ruby script outputs the following on Ruby 2.2.6:

[“a”, “b”, “c”, [“d”, “e”, “f”]]
[“a”, “b”, “c”, “d”, “e”, “f”]
[“a”, “b”, “c”, [“d”, “e”, “f”], false, false]

The expected results would be [“a”, “b”, “c”, “d”, “e”, “f”]. Anyone
knows how to do this with either Fiddle or FFI (or both)?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs