# Help with rb\_iv\_set

**URL:** <https://www.ruby-forum.com/t/help-with-rb-iv-set/136647>\
**Category:** Ruby\
**Created:** [May 5, 2008, 5:13am UTC](https://www.ruby-forum.com/t/help-with-rb-iv-set/136647 "2008-05-05T05:13:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [May 5, 2008, 5:13am UTC](https://www.ruby-forum.com/t/help-with-rb-iv-set/136647/1 "2008-05-05T05:13:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [May 5, 2008, 5:30am UTC](https://www.ruby-forum.com/t/help-with-rb-iv-set/136647/2 "2008-05-05T05:30:18Z")

</div>

On Sun, May 4, 2008 at 11:13 PM, Bryan R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [May 5, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/help-with-rb-iv-set/136647/3 "2008-05-05T15:36:51Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [May 5, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/help-with-rb-iv-set/136647/4 "2008-05-05T19:45:30Z")

</div>

Perfect… :). Thanks Tim!

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [May 5, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/help-with-rb-iv-set/136647/5 "2008-05-05T18:36:21Z")

</div>

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
