# Ruby with C

**URL:** <https://rubytalk.org/t/ruby-with-c/36420>\
**Category:** ruby-talk\
**Created:** [24 March 2007 23:22 UTC](https://rubytalk.org/t/ruby-with-c/36420 "2007-03-24T23:22:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dogwasstar](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@Dogwasstar](https://rubytalk.org/u/Dogwasstar)\
**Post date:** [24 March 2007 23:22 UTC](https://rubytalk.org/t/ruby-with-c/36420/1 "2007-03-24T23:22:15Z")

</div>

I am new at programming ruby with C. I am trying to pass in a value from  
a test ruby program to my C class and trying to print it out. But when i  
print it out i am getting the wrong number printerd out.

Here is what i am trying to do.

Here is the ruby test file

class TestTest \< Test::Unit::TestCase  
&nbsp;&nbsp;def test\_test  
&nbsp;&nbsp;&nbsp;&nbsp;k = PKey.new('4')  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(Object, PKey.superclass)  
&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(PKey, k.class)  
&nbsp;&nbsp;&nbsp;&nbsp;trtying = k.bit(5)  
&nbsp;&nbsp;end  
end

Here is the C file

static VALUE t\_bit(VALUE self, VALUE obj)  
{  
&nbsp;&nbsp;&nbsp;&nbsp;VALUE n;

&nbsp;&nbsp;&nbsp;&nbsp;n = NUM2INT(obj);  
&nbsp;&nbsp;&nbsp;&nbsp;printf("Fun %d", INT2NUM(n));  
&nbsp;&nbsp;&nbsp;&nbsp;return Qnil;  
}

Any suggestion would be helpfull. Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Harold\_Hausman](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@Harold\_Hausman](https://rubytalk.org/u/Harold_Hausman)\
**Post date:** [24 March 2007 23:54 UTC](https://rubytalk.org/t/ruby-with-c/36420/2 "2007-03-24T23:54:57Z")

</div>

Hi.

By putting the variable 'n' through the INT2NUM macro as you pass it  
into printf you're re-converting it back into a Ruby VALUE which is  
not what you want to do. It will give you unpredictable (or at least  
more-difficult-to-predict 😉 ) results...

Assuming you just want to print the value of 'obj' as an integer, you  
should probably declare 'n' as an integer instead of as a VALUE, and  
use the NUM2INT (as you have) to convert the Ruby VALUE 'obj' into a  
plain int. Then you can pass it straight into printf (as you would  
with any other int) ... no need for INT2NUM there.

hope that helps,  
-Harold

> **···**
>
> On 3/25/07, dogwasstar@hotmail.com \<dogwasstar@hotmail.com\> wrote:
> 
> > Here is the C file
> > 
> > static VALUE t\_bit(VALUE self, VALUE obj)  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;VALUE n;
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;n = NUM2INT(obj);  
> > &nbsp;&nbsp;&nbsp;&nbsp;printf("Fun %d", INT2NUM(n));  
> > &nbsp;&nbsp;&nbsp;&nbsp;return Qnil;  
> > }

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [25 March 2007 18:31 UTC](https://rubytalk.org/t/ruby-with-c/36420/3 "2007-03-25T18:31:28Z")

</div>

Your question has already been answered. Alternatively, use inline to learn without all the hassle (no makefile, no builds, no type conversions, no time wasted). Just try out what you want to do straight up in ruby with inlined C, and then go look at what the C code is producing:

#!/usr/local/bin/ruby

require 'rubygems'  
require 'inline'

class PKey  
&nbsp;&nbsp;&nbsp;inline do |builder|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;builder.c \<\<-EOM  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static void bit(int n) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("Fun %d\\n", n);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EOM  
&nbsp;&nbsp;&nbsp;end  
end

k = PKey.new  
k.bit(5)

> **···**
>
> ############################################################  
> # Produces:  
> #  
> # #include "ruby.h"  
> #  
> # # line 9 "./blah.rb"  
> # static VALUE bit(VALUE self, VALUE \_n) {  
> # int n = FIX2INT(\_n);  
> #  
> # printf("Fun %d\n", n);  
> # return Qnil;  
> # }  
> #  
> # #ifdef \_\_cplusplus  
> # extern "C" {  
> # #endif  
> # void Init\_Inline\_PKey\_f671() {  
> # VALUE c = rb\_cObject;  
> # c = rb\_const\_get\_at(c,rb\_intern("PKey"));  
> # rb\_define\_method(c, "bit", (VALUE(\*)(ANYARGS))bit, 1);  
> #  
> # }  
> # #ifdef \_\_cplusplus  
> # }  
> # #endif

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [25 March 2007 00:16 UTC](https://rubytalk.org/t/ruby-with-c/36420/4 "2007-03-25T00:16:08Z")

</div>

You mean C++ class?  
C doesn't have objects or classes.

> **···**
>
> On Mar 25, 2007, at 8:54 AM, Harold Hausman wrote:
> 
> > On 3/25/07, dogwasstar@hotmail.com \<dogwasstar@hotmail.com\> wrote:
> > 
> > > Here is the C file
> > > 
> > > static VALUE t\_bit(VALUE self, VALUE obj)  
> > > {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;VALUE n;
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;n = NUM2INT(obj);  
> > > &nbsp;&nbsp;&nbsp;&nbsp;printf("Fun %d", INT2NUM(n));  
> > > &nbsp;&nbsp;&nbsp;&nbsp;return Qnil;  
> > > }
> > 
> > Hi.
> > 
> > By putting the variable 'n' through the INT2NUM macro as you pass it  
> > into printf you're re-converting it back into a Ruby VALUE which is  
> > not what you want to do. It will give you unpredictable (or at least  
> > more-difficult-to-predict 😉 ) results...
> > 
> > Assuming you just want to print the value of 'obj' as an integer, you  
> > should probably declare 'n' as an integer instead of as a VALUE, and  
> > use the NUM2INT (as you have) to convert the Ruby VALUE 'obj' into a  
> > plain int. Then you can pass it straight into printf (as you would  
> > with any other int) ... no need for INT2NUM there.
> > 
> > hope that helps,  
> > -Harold

---

<div class="post-metadata">

**Author:** ![Patrick\_Hurley1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Patrick\_Hurley1](https://rubytalk.org/u/Patrick_Hurley1)\
**Post date:** [25 March 2007 02:16 UTC](https://rubytalk.org/t/ruby-with-c/36420/5 "2007-03-25T02:16:48Z")

</div>

> \> Here is the C file  
> \>  
> \> static VALUE t\_bit(VALUE self, VALUE obj)  
> \> {

&nbsp;&nbsp;&nbsp;&nbsp;int n = NUM2INT(obj);  
&nbsp;&nbsp;&nbsp;&nbsp;printf("Fun %d", n);

> \> return Qnil;  
> \> }  
> hope that helps,  
> -Harold

Exactly what Harold said, but a little code in case that helps.

pth

> **···**
>
> On 3/24/07, Harold Hausman \<hhausman@gmail.com\> wrote:
> 
> > On 3/25/07, dogwasstar@hotmail.com \<dogwasstar@hotmail.com\> wrote:
