Native extension: arguments as a hash

I’m working on a native extension in C for Ruby 1.8.7. The method I’m
working on must have the signature:

def receive(receiver, opts = {})

so that users can call:

receiver(receiver, :timeout => 15) # :timeout is optional

But I can’t find a decent example of what such a method would look like
in C. I’ve tried the following:

static VALUE qpid_receiver(VALUE self, VALUE* varg)
{
}

which works fine for receiving variable arguments. But what I’m not
seeing is the arguments beyond the first being pushed into a Hash.

So the question is: in a native extension, how do I tell Ruby that I
want all arguments after the first to be presented as a Hash, with a
default of an empty Hash if no arguments are present?

Thanks.

On Fri, Dec 2, 2011 at 14:14, Darryl L. Pierce [email protected]
wrote:

in C.
We have a function for this in Ruby-GNOME2:

https://github.com/ruby-gnome2/ruby-gnome2/blob/master/glib2/ext/glib2/rbglib.c#L838

You can see it in use here:

https://github.com/ruby-gnome2/ruby-gnome2/blob/0c2ea5ec150c936e00d079ca62501131e32def95/vte/ext/vte/rbvte-terminal.c#L111

On 12/02/2011 08:30 AM, Nikolai W. wrote:

But I can’t find a decent example of what such a method would look like
in C.

We have a function for this in Ruby-GNOME2:

https://github.com/ruby-gnome2/ruby-gnome2/blob/master/glib2/ext/glib2/rbglib.c#L838

You can see it in use here:

https://github.com/ruby-gnome2/ruby-gnome2/blob/0c2ea5ec150c936e00d079ca62501131e32def95/vte/ext/vte/rbvte-terminal.c#L111

Thank you! I’m looking it over now. :slight_smile:

Darryl Pierce wrote in post #1034735:

I’m working on a native extension in C for Ruby 1.8.7. The method I’m
working on must have the signature:

def receive(receiver, opts = {})

so that users can call:

receiver(receiver, :timeout => 15) # :timeout is optional

But I can’t find a decent example of what such a method would look like
in C. I’ve tried the following:

static VALUE qpid_receiver(VALUE self, VALUE* varg)
{
}

Hi,

I am using 1.9.2, but hopefully it doesn’t change much. To have command
with default values you can declare your function as

rb_define_method(yourCclass, “receive”, (VALUE(*)(…))qpid_receiver,
-1);

(Notice the -1; it means it’s variable arguments). Then you can define
your function as

static VALUE qpid_receiver(int argc, VALUE* argv, VALUE self)
{
VALUE rcvr, opts;
rb_scan_args(argc, argv, “11”, &rcvr, &opts); // one has to be given,
// one optional
if (NIL_P(opts)) // if opts is not given
opts = rb_hash_new(); // make it a hash; maybe in 1.8.7 new hash is
// created differently

// Continued with the rest…
}

I haven’t compile the code, so maybe there are some typos. Also, beside
-1, you can also have -2 for variable arguments, but the syntax is
rather different. Hope this helps.

Regards,

Bill

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