Any "easy" example of using T_HASH in a C extension?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, I've found some tutorials explaining how to create and manipulat a Ruby=
=20
array from a C extension using T_ARRAY and its realted functions.
However I find nothing about T_HASH, and trying to extract the functions fr=
om=20
Ruby "hash.c" code is... not very cool.

Is there any tutorial about creating a simple hash in Ruby C extension?

Thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
A

Aaron Patterson

Hi, I've found some tutorials explaining how to create and manipulat =20=
a Ruby
array from a C extension using T_ARRAY and its realted functions.
However I find nothing about T_HASH, and trying to extract the =20
functions from
Ruby "hash.c" code is... not very cool.

Sure it is! All of the hipsters at Beauty Bar were talking about it =20
the other night. But you'd better hop on board quickly before it =20
becomes uncool, otherwise you're going to have to wait until it =20
becomes "retro cool".
Is there any tutorial about creating a simple hash in Ruby C =20
extension?

rb_hash_new(void)
rb_hash_aset(VALUE hash, VALUE key, VALUE val)
rb_hash_aref(VALUE hash, VALUE key)

Though, if you're just doing hash manipulation, why not just do it in =20=

Ruby?
 
I

Iñaki Baz Castillo

El Lunes, 12 de Octubre de 2009, Aaron Patterson escribi=F3:
=20
Sure it is! All of the hipsters at Beauty Bar were talking about it
the other night. But you'd better hop on board quickly before it
becomes uncool, otherwise you're going to have to wait until it
becomes "retro cool".

=20
rb_hash_new(void)
rb_hash_aset(VALUE hash, VALUE key, VALUE val)
rb_hash_aref(VALUE hash, VALUE key)

Yeah! just found these functions in an online presentation. :)
=20
Though, if you're just doing hash manipulation, why not just do it in
Ruby?

Not exactly. I've coded a parser in C and it must return a Ruby hash.

Thanks a lot.



=2D-=20
I=F1aki Baz Castillo <[email protected]>
 
D

Dirkjan Bussink

Hi, I've found some tutorials explaining how to create and manipulat =20=
a Ruby
array from a C extension using T_ARRAY and its realted functions.
However I find nothing about T_HASH, and trying to extract the =20
functions from
Ruby "hash.c" code is... not very cool.

I'd advise against using T_ARRAY, RARRAY_PTR etc, writing stuff with =20
the following methods makes life a lot easier for other Ruby =20
implementations that also support MRI C extensions:

rb_ary_new(void);
rb_ary_push(VALUE array, VALUE element);
rb_ary_entry(VALUE array, long i);
rb_ary_store(VALUE array, long i, VALUE element);

--=20
Regards,

Dirkjan Bussink
 
I

Iñaki Baz Castillo

El Lunes, 12 de Octubre de 2009, Dirkjan Bussink escribi=F3:
=20
I'd advise against using T_ARRAY, RARRAY_PTR etc, writing stuff with
the following methods makes life a lot easier for other Ruby
implementations that also support MRI C extensions:
=20
rb_ary_new(void);
rb_ary_push(VALUE array, VALUE element);
rb_ary_entry(VALUE array, long i);
rb_ary_store(VALUE array, long i, VALUE element);

Yes, finally I used just "rb_hash_new" and "rb_hash_aset" functions.


=2D-=20
I=F1aki Baz Castillo <[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top