I’m struggling to write a module, with a method, in C.
With plain Ruby, I would do something like this:
module M
def M.five
return 5
end
end
=> nil
M.five
=> 5
So I thought that, in C, I could write:
#include “science.h”
static VALUE mScience_seven(VALUE self) { return rb_float_new(7); }
VALUE mScience;
void Init_science() { /* Module definition */
mScience = rb_define_module(“Science”);
mScience_define_constants();
rb_define_method(mScience, “Science.seven”, mScience_seven, 0);
}
The module itself builds, and I can access the constants fine. But the
method is giving me trouble:
require ‘science’
=> true
Science::SOLAR_MASS
=> 1.98892e+30
Science.seven
NameError: undefined method `seven’ for Science:Module
from (irb):3
I’ve also tried rb_define_method(…“seven”…), but that didn’t work
either.
Does anyone know how I can write module methods in C?
Thanks.
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
You want rb_define_module_function:
void rb_define_module_function(VALUE classmod, char *name, VALUE(*func()),
int argc);
See the Pickaxe, chapter 17, page 190.
···
On Sat, 25 Jan 2003 07:59:35 +0900, Daniel Carrera wrote:
I’m struggling to write a module, with a method, in C.
With plain Ruby, I would do something like this:
module M
def M.five
return 5
end
end
=> nil
M.five
=> 5
So I thought that, in C, I could write:
#include “science.h”
static VALUE mScience_seven(VALUE self) { return rb_float_new(7); }
VALUE mScience;
void Init_science() { /* Module definition */
mScience = rb_define_module(“Science”);
mScience_define_constants();
rb_define_method(mScience, “Science.seven”, mScience_seven, 0);
}
The module itself builds, and I can access the constants fine. But the
method is giving me trouble:
require ‘science’
=> true
Science::SOLAR_MASS
=> 1.98892e+30
Science.seven
NameError: undefined method `seven’ for Science:Module
from (irb):3
I’ve also tried rb_define_method(…“seven”…), but that didn’t work
either.
Does anyone know how I can write module methods in C?
Thanks.
Okay, this is getting ridiculous. I swear that my code looks exactly like
the Ruby source. Could someone help me out? Ruby can’t even see the
function being declared. I thinks it just doesn’t exist.
Please help.
C source:
#include “science.h”
VALUE mScience;
static VALUE mScience_seven(VALUE self) {
return rb_float_new(7);
}
/* Module definition */
void Init_Science() {
mScience = rb_define_module(“Science”);
mScience_define_constants();
rb_define_module_function(mScience, “seven”, mScience_seven, 0);
}
IRB:
require ‘science’
=> true
Science::seven
NameError: undefined method `seven’ for Science:Module
from (irb):2
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
No. But why are you writing a module of mathematical constants in C?
Sounds like a job for Ruby to me!
Gavin
···
On Saturday, January 25, 2003, 9:59:35 AM, Daniel wrote:
Does anyone know how I can write module methods in C?
You want rb_define_module_function:
void rb_define_module_function(VALUE classmod, char *name, VALUE(*func()),
int argc);
See the Pickaxe, chapter 17, page 190.
Yes, I tried that too, and it doesn’t work either.
C code:
rb_define_module_function(mScience, “Science.seven”, mScience_seven, 0);
IRB:
Science::SOLAR_MASS
=> 1.98892e+30
Science.seven
NameError: undefined method `seven’ for Science:Module
from (irb):3
Perhaps I just don’t know the syntax. Does anyone know if I’m using these
functions wrong?
···
On Sat, Jan 25, 2003 at 09:10:15AM +0900, Tim Hunter wrote:
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
Well, the constants parts is not what’s giving me trouble. That was easy.
It’s the “real meat” that I had trouble with. But I got some assistance,
so I’m moving along now.
But you are right, it is kind of silly to do that in C. I’ll switch it to
Ruby later.
···
On Sat, Jan 25, 2003 at 06:24:02PM +0900, Gavin Sinclair wrote:
Does anyone know how I can write module methods in C?
No. But why are you writing a module of mathematical constants in C?
Sounds like a job for Ruby to me!
Gavin
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
Just specify the name: “seven”. Omit the module name.
This code:
rb_define_module_function(mScience, “seven”, mScience_seven, 0);
should work.
···
On Sat, 25 Jan 2003 09:20:36 +0900, Daniel Carrera dcarrera@math.umd.edu wrote:
Perhaps I just don’t know the syntax. Does anyone know if I’m using these
functions wrong?
Tim,
Thanks for your help. The problem was that earlier one I had done a
"make install" with the wrong code and ruby was using that. That’s why I
kept getting errors after I made the corrections that you told me.
Now that I fixed this it works fine.
Thanks.
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137