I've been using Python for a long time and I've just started to toy around with Ruby, but I'm finding the differences between modules in the two languages really confusing, especially when viewed through their C-APIs. For example, here is a small ruby C module
#include "Python.h"
#include "ruby.h"
VALUE PyModule = Qnil;
void Init_pymodule();
VALUE method_Py_Initialize();
VALUE method_PyRun_SimpleString();
void Init_pymodule() {
PyModule = rb_define_module("PyModule");
rb_define_method(PyModule, "Py_Initialize", method_Py_Initialize, 0);
rb_define_method(PyModule, "PyRun_SimpleString", method_PyRun_SimpleString, 1);
}
VALUE method_Py_Initialize(VALUE self) {
Py_Initialize();
return Qnil;
}
VALUE method_PyRun_SimpleString(VALUE self, VALUE command) {
return INT2NUM(PyRun_SimpleString(RSTRING(command)->as.ary));
}
When I import and call that like so
require "pymodule"
PyModule.Py_Initialize()
PyModule.PyRun_SimpleString("print 'hellooo'")
I expected it to work similar to how python's modules would work. However, I am finding that I have to either use PyModule
to extend a class or I must use include "PyModule"
. I'm looking for a way to have PyModule
attach these functions to the module object similar to Python's semantics. I'm also curious what is actually happening in Ruby that gives it this behavior.