From: ts Date: 2001-03-13T02:28:35+09:00 Subject: [ruby-talk:12515] Re: rb_ary_each and hash >>>>> "U" == User Tcovert writes: U> is there an example of how to use the rb_ary_each(VALUE) function. I U> know how to use it in ruby, but not sure how that would work in c. I've not understood for Array you can make : for (i = 0; RARRAY(ary)->len; i++) { /* do what you want with RARRAY(ary)->ptr[i] */ } U> I have a hash of string-values, I'd like to make a c array of cstr's, I U> was wondering how I can iterate through the hash to pull out all the U> values. I can figure out how I would do this with an array(I think) but U> looking in ruby.h and internal.h I can't see any operators that would U> work with the Hash. I know it mixes in enumerable, but I'm not sure how U> that affects the c layer. Here a little, and stupid, example to iterate with rb_iterate() pigeon% cat tt.c #include static VALUE tt_puts_i(obj, tt) VALUE obj, tt; { VALUE key, value; key = rb_ary_entry(obj, 0); value = rb_ary_entry(obj, 1); rb_warn("%d %d %d", NUM2INT(key), NUM2INT(value), NUM2INT(tt)); return Qtrue; } static VALUE tt_hash(hash, obj) VALUE hash, obj; { rb_iterate(rb_each, hash, tt_puts_i, INT2FIX(12)); return Qtrue; } void Init_tt() { rb_define_method(rb_cHash, "toto", tt_hash, 0); } pigeon% pigeon% ruby -rtt -e '{12 =>12, 24 => 34}.toto' -e:1: warning: 24 34 12 -e:1: warning: 12 12 12 pigeon% Guy Decoux