[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help with rb_iv_set

Bryan Richardson

5/5/2008 3:13:00 AM

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
--
Posted via http://www.ruby-....

4 Answers

Rick DeNatale

5/5/2008 3:29:00 AM

0

On Sun, May 4, 2008 at 11:13 PM, Bryan Richardson
<btrichardson@gmail.com> wrote:
> 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;
}


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Bryan Richardson

5/5/2008 1:36:00 PM

0

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

--
Thanks!
Bryan

On Sun, May 4, 2008 at 9:29 PM, Rick DeNatale <rick.denatale@gmail.com>
wrote:

> On Sun, May 4, 2008 at 11:13 PM, Bryan Richardson
> <btrichardson@gmail.com> wrote:
> > 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;
> }
>
>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...
>
>

Tim Pease

5/5/2008 4:35:00 PM

0


On May 5, 2008, at 7:35 AM, Bryan Richardson 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


> --
> Thanks!
> Bryan
>
> On Sun, May 4, 2008 at 9:29 PM, Rick DeNatale
> <rick.denatale@gmail.com>
> wrote:
>
>> On Sun, May 4, 2008 at 11:13 PM, Bryan Richardson
>> <btrichardson@gmail.com> wrote:
>>> 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;
>> }
>>
>>
>> --
>> Rick DeNatale
>>
>> My blog on Ruby
>> http://talklikeaduck.denh...
>>
>>


Bryan Richardson

5/5/2008 5:45:00 PM

0

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

Perfect... :). Thanks Tim!

On Mon, May 5, 2008 at 10:34 AM, Tim Pease <tim.pease@gmail.com> wrote:

>
> On May 5, 2008, at 7:35 AM, Bryan Richardson 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
>
>
>
> --
> > Thanks!
> > Bryan
> >
> > On Sun, May 4, 2008 at 9:29 PM, Rick DeNatale <rick.denatale@gmail.com>
> > wrote:
> >
> > On Sun, May 4, 2008 at 11:13 PM, Bryan Richardson
> > > <btrichardson@gmail.com> wrote:
> > >
> > > > 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;
> > > }
> > >
> > >
> > > --
> > > Rick DeNatale
> > >
> > > My blog on Ruby
> > > http://talklikeaduck.denh...
> > >
> > >
> > >
>
>