Reputation: 1689
I'm currently testing c-extending ruby. The following test-module compiles and gets installed:
# include "ruby.h"
static VALUE t_init(VALUE self)
{
return self;
}
static VALUE t_check(VALUE self)
{
return 15;
}
VALUE Qmodule;
VALUE FlagValueClass;
void Init_Flags()
{
Qmodule = rb_define_module("Q");
FlagValueClass = rb_define_class_under(Qmodule, "Flags", rb_cObject);
rb_define_method(FlagValueClass, "initialize", t_init, 0);
rb_define_method(FlagValueClass, "check", t_check, 0);
}
But when I load it into irb:
1.9.3-p286 :002 > require 'Q/Flags'
=> true
1.9.3-p286 :003 > a = Q::Flags
=> Q::Flags
1.9.3-p286 :004 > a = Q::Flags.new
=> #<Q::Flags:0x00000001fa78e0>
1.9.3-p286 :005 > puts a.check()
=> 7
…I get 7
instead of 15
which I would expect. Could someone please explain what is happening here?
Upvotes: 0
Views: 68
Reputation: 1689
OK; after trying to return a string and getting a dump, i finally got it … ^^
My fault was to not read the documentation (especially the macros in ruby.h
) carefully: by returning 15, Ruby gets 0xF
wich has the LSB set. Therefore it sees a fixnum, wich gets its value by shifting it one bit right, so it ends as 0x7
. By returning INT2FIX(15)
instead of just 15
, this gets solved.
Upvotes: 2