# Ruby with C

**URL:** <https://www.ruby-forum.com/t/ruby-with-c/90014>\
**Category:** Ruby\
**Created:** [March 25, 2007, 12:22am UTC](https://www.ruby-forum.com/t/ruby-with-c/90014 "2007-03-25T00:22:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 25, 2007, 12:22am UTC](https://www.ruby-forum.com/t/ruby-with-c/90014/1 "2007-03-25T00:22:13Z")

</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  
def test\_test  
k = PKey.new(‘4’)  
assert\_equal(Object, PKey.superclass)  
assert\_equal(PKey, k.class)  
trtying = k.bit(5)  
end  
end

Here is the C file

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

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

```

}

Any suggestion would be helpfull. Thanks

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 25, 2007, 12:56am UTC](https://www.ruby-forum.com/t/ruby-with-c/90014/2 "2007-03-25T00:56:10Z")

</div>

On 3/25/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

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:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 25, 2007, 1:17am UTC](https://www.ruby-forum.com/t/ruby-with-c/90014/3 "2007-03-25T01:17:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 25, 2007, 4:17am UTC](https://www.ruby-forum.com/t/ruby-with-c/90014/4 "2007-03-25T04:17:18Z")

</div>

On 3/24/07, Harold H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 3/25/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Here is the C file
> > 
> > static VALUE t\_bit(VALUE self, VALUE obj)  
> > {

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

```

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

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

pth

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [March 25, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/ruby-with-c/90014/5 "2007-03-25T20:32:00Z")

</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  
inline do |builder|  
builder.c \<\<-EOM  
static void bit(int n) {  
printf(“Fun %d\n”, n);  
}  
EOM  
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
