From: Bryan Richardson Date: 2008-05-06T03:48:00+09:00 Subject: Re: Need more help with Ruby and C ------=_Part_20782_11749811.1210009460652 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Jan, Awesome... this now works perfectly! Also, thanks for the heads-up on having to return a value and using Qnil. I was getting a segmentation fault in irb that I wasn't sure about and it was because I wasn't returning anything. One side question... why is it called 'Qnil'? What is the 'Q' for? -- Thanks again! Bryan On Mon, May 5, 2008 at 10:38 AM, Jan Dvorak wrote: > On Monday 05 May 2008 15:44:03 Bryan Richardson wrote: > > Hi Jan, > > > > Alright, that makes sense, thanks for the information. However, that > > doesn't help me to understand how I can pass a Ruby array of objects to > the > > execute method. Am I missing something here? Can you help me with > this? > > > The array is just an object (everything in ruby is object) which you can > manipulate from C with rb_ary_* functions. You wrote the code yourself: > > VALUE foo = rb_ary_entry(foos, 0); // this will return the first element > of > the array > > now, rb_* functions operate on (and returns) ruby objects (VALUE), if you > want > to direclty use POD data types in C, you have to convert them, e.g. : > > VALUE val = rb_iv_get(foo,"@value"); > printf("Foo Value: %i\n", NUM2INT(val)); > > or alternatively > > printf("Foo Value: %i\n", NUM2INT(rb_iv_get(foo, "@value")); > > For integral types there are general macros INT2NUM (c to ruby) and > NUM2INT > (ruby to C), and their variants (FIX2INT,INT2FIX,..), for floats/doubles > there is NUM2DBL (ruby to c) and rb_float_new() (C to ruby). > > Also, every method in ruby must return value, so if your function does not > return anything, you have to explicitly return 'nil' with > > return Qnil; > > Jan > > ------=_Part_20782_11749811.1210009460652--