D
Daniel Berger
Hi all,
I'm writing an extension, and I'm having a little trouble with using a
hash for keyword arguments. I want to allow a API like this:
Foo.test(
:bar => "hello",
'baz' => "world"
)
In the extension to get the key value I do this:
VALUE rbBar = rb_hash_aref(myHash,rb_str_new2("bar"));
If the key "bar" is a string, that works fine. However, that returns
nil (not a symbol) if "bar" is a symbol. I know I can do rb_iterate
and I can detect if hash keys are symbols, but I don't know how to
permanately change the key back to a string, i.e. it appears to pass a
copy of the hash, rather than a reference.
// Iterate over hash. Assume that rb_sym2str() works properly.
static VALUE parse_hash(VALUE array, VALUE class)
{
VALUE key, tkey, val;
key = rb_ary_entry(array, 0); // Get key
val = rb_ary_entry(array, 1); // Get value
if(TYPE(key) == T_SYMBOL){
key = rb_sym2str(key); // Convert T_SYMBOL to T_STRING
printf("Key is now: %s\n",STR2CSTR(key)); // Verify string
rb_ary_store(array,0,key); // Doesn't work
}
return array;
}
Any ideas?
Regards,
Dan
I'm writing an extension, and I'm having a little trouble with using a
hash for keyword arguments. I want to allow a API like this:
Foo.test(
:bar => "hello",
'baz' => "world"
)
In the extension to get the key value I do this:
VALUE rbBar = rb_hash_aref(myHash,rb_str_new2("bar"));
If the key "bar" is a string, that works fine. However, that returns
nil (not a symbol) if "bar" is a symbol. I know I can do rb_iterate
and I can detect if hash keys are symbols, but I don't know how to
permanately change the key back to a string, i.e. it appears to pass a
copy of the hash, rather than a reference.
// Iterate over hash. Assume that rb_sym2str() works properly.
static VALUE parse_hash(VALUE array, VALUE class)
{
VALUE key, tkey, val;
key = rb_ary_entry(array, 0); // Get key
val = rb_ary_entry(array, 1); // Get value
if(TYPE(key) == T_SYMBOL){
key = rb_sym2str(key); // Convert T_SYMBOL to T_STRING
printf("Key is now: %s\n",STR2CSTR(key)); // Verify string
rb_ary_store(array,0,key); // Doesn't work
}
return array;
}
Any ideas?
Regards,
Dan