Help with rb_iv_set

Hello all,

I’m trying to use rb_iv_set to set a double value in a C extension I’m
writing.

rb_iv_set(self, “@voltage”, 1.01);

This doesn’t seem to work, as I get a ‘0’ instead of ‘1.01’. If I use
rb_float_new(1.01), then it works. However, the voltage value in my C
structure is a double, and rb_double_new doesn’t exist.

Any suggestions?


Thanks!
Bryan

On Sun, May 4, 2008 at 11:13 PM, Bryan R.
[email protected] wrote:

Any suggestions?

rb_iv_set needs a VALUE for the third parameter.
and the value of a Ruby float is a C double:

From the 1.8.6 source:

in ruby.h

VALUE rb_iv_set _((VALUE, const char*, VALUE));

and also:

struct RFloat {
struct RBasic basic;
double value;
};

And from numeric.c

VALUE
rb_float_new(d)
double d;
{
NEWOBJ(flt, struct RFloat);
OBJSETUP(flt, rb_cFloat, T_FLOAT);

flt->value = d;
return (VALUE)flt;

}


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Hi Rick,

So you’re saying that by doing it the way I was doing it (using
rb_float_new) I am indeed doing it correctly?


Thanks!
Bryan

On Sun, May 4, 2008 at 9:29 PM, Rick DeNatale [email protected]

Perfect… :). Thanks Tim!

On May 5, 2008, at 7:35 AM, Bryan R. wrote:

Hi Rick,

So you’re saying that by doing it the way I was doing it (using
rb_float_new) I am indeed doing it correctly?

To be explicit about your specific case …

rb_iv_set(self, “@voltage”, rb_float_new(1.01));

Blessings,
TwP

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs