Help with rb_iv_set

  • Thread starter Bryan Richardson
  • Start date
B

Bryan Richardson

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?
 
R

Rick DeNatale

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?

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;
}
 
B

Bryan Richardson

[Note: parts of this message were removed to make it a legal post.]

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?
 
T

Tim Pease

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
 
B

Bryan Richardson

[Note: parts of this message were removed to make it a legal post.]

Perfect... :). Thanks Tim!
 

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
474,204
Messages
2,571,066
Members
47,672
Latest member
svaraho

Latest Threads

Top