D
Daniel Waite
I want to code a few methods in C for speed reasons. The Pick Axe book
(2nd edition) is a wonderful guide to writing Ruby code in C, but that's
not what I want. The benchmarks I ran for the Ruby-in-Ruby and Ruby-in-C
programs showed similar results. So allow me to clarify:
I do not want a Ruby array in C. I want a C array in C.
Which I have done...
int array[1000000];
static VALUE t_add(VALUE self, VALUE obj, VALUE i)
{
array = obj;
return 1;
}
static VALUE t_include(VALUE self, VALUE obj)
{
int size = sizeof(array) / sizeof(int);
int index;
for(index = 0; index < size; index++) {
if(FIX2INT(obj) == FIX2INT(array[index])) {
return Qtrue;
}
}
return Qfalse;
}
static VALUE t_at(VALUE self, VALUE obj)
{
return array[obj];
}
The value of "obj" comes from within Ruby (IRB, actually). But unless I
coerce it using FIX2INT, it's always a weird value. Say I put in 1,
it'll tell me it received 31478 or something similar.
Another thing... I thought that if I passed in an 'a' or any other
non-integer value into the add method, I'd get an error. I don't. In
fact...
require 'my_test'
mt = MyTest.new
mt.add('a', 0)
mt.at(0) # 'a'
But...
mt.include?('a') # false
So... what's happening to my arguments as they pass through Ruby and
into C?
(2nd edition) is a wonderful guide to writing Ruby code in C, but that's
not what I want. The benchmarks I ran for the Ruby-in-Ruby and Ruby-in-C
programs showed similar results. So allow me to clarify:
I do not want a Ruby array in C. I want a C array in C.
Which I have done...
int array[1000000];
static VALUE t_add(VALUE self, VALUE obj, VALUE i)
{
array = obj;
return 1;
}
static VALUE t_include(VALUE self, VALUE obj)
{
int size = sizeof(array) / sizeof(int);
int index;
for(index = 0; index < size; index++) {
if(FIX2INT(obj) == FIX2INT(array[index])) {
return Qtrue;
}
}
return Qfalse;
}
static VALUE t_at(VALUE self, VALUE obj)
{
return array[obj];
}
The value of "obj" comes from within Ruby (IRB, actually). But unless I
coerce it using FIX2INT, it's always a weird value. Say I put in 1,
it'll tell me it received 31478 or something similar.
Another thing... I thought that if I passed in an 'a' or any other
non-integer value into the add method, I'd get an error. I don't. In
fact...
require 'my_test'
mt = MyTest.new
mt.add('a', 0)
mt.at(0) # 'a'
But...
mt.include?('a') # false
So... what's happening to my arguments as they pass through Ruby and
into C?