Forum: Ruby Using FFI or Fiddle on an Embedded Ruby

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ian T. (Guest)
on 2017-03-20 17:50
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

   << 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 topic is locked and can not be replied to.