From: Admin Tensor Date: 2011-12-02T23:17:03+09:00 Subject: Re: Native extension: arguments as a hash 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 -- Posted via http://www.ruby-forum.com/.