Help needed: extending ruby

I’m trying out the following example from
the book “Making use of Ruby” (page 200,201):

#----- tst_simple.rb ------------
require ‘simple’

s = Simple.new(123)

p s.i
p s.a

s.i = 456
s.a.push("Hello")
s.a.push("World")

p s.a
···

#-----------------------------

where the class Simple is defined using the C extension shown below.
I compiled it using VC++ 6.0 on Win XP Pro and got no warning or errors.
Then I copied the resulting “simple.dll” in C:\ruby\lib\ruby\site_ruby.

But when I ran the ruby code I get:

C:/ruby/lib/ruby/site_ruby/simple.dll: 126: The specified module could not
be found. - C:/ruby/lib/ruby/site_ruby/simple.dll (LoadError)
from C:/atest/tst_simple.rb:1

What am I missing ?

//---------------------------------------------------------------------
#include <ruby.h>

static VALUE simple_initialize(VALUE self, int iValue)
{
rb_iv_set(self,"@i",iValue);
rb_iv_set(self,"@a",rb_ary_new());
return self;
}

static VALUE simple_get_i(VALUE self)
{
return rb_iv_get(self,"@i");
}

static VALUE simple_set_i(VALUE self, int iValue)
{
return rb_iv_set(self,"@i",iValue);
}

static VALUE simple_get_a(VALUE self)
{
return rb_iv_get(self,"@a");
}

void Init_Simple()
{
VALUE cSimple;

cSimple = rb_define_class(“Simple”,rb_cObject);
rb_define_method(cSimple,“initialize”,simple_initialize,1);

rb_define_method(cSimple,“i”, simple_get_i, 0);
rb_define_method(cSimple,“i=”, simple_set_i, 1);
rb_define_method(cSimple,“a”, simple_get_a, 0);
}

//---------------------------------------------------------------------

Please help. Thanks,
– shanko

Hey,

Shashank Date wrote:

I’m trying out the following example from
the book “Making use of Ruby” (page 200,201):

void Init_Simple()

Since your module meant to be named ‘simple’, you should use this line
instead:

void Init_simple()

According to pickaxe 1:

Now look at the last function, Init_Test. Every class or module defines
a C global function named Init_ Name. This function will be called when
the interpreter first loads the extension Name (or on startup for
statically linked extensions). It is used to initialize the extension
and to insinuate it into the Ruby environment.

Hope this helps,

···


Laurent

I’m trying out the following example from
the book “Making use of Ruby” (page 200,201):

[snip example]

The Ruby-example code looks OK.

where the class Simple is defined using the C extension shown below.
I compiled it using VC++ 6.0 on Win XP Pro and got no warning or errors.
Then I copied the resulting “simple.dll” in C:\ruby\lib\ruby\site_ruby.

It is not necessary to copy the dll. Ruby looks in the current directory
after the dll.

But when I ran the ruby code I get:

C:/ruby/lib/ruby/site_ruby/simple.dll: 126: The specified module could not
be found. - C:/ruby/lib/ruby/site_ruby/simple.dll (LoadError)
from C:/atest/tst_simple.rb:1

It seems to me that Ruby loads your file… but cannot locate any
bootstrapping routine. Ruby is case-sensitive.

void Init_Simple()
^^^
^^^

Try rename ‘Simple’ into ‘simple’
void Init_simple()

Alternatively (the oposite solution): rename the dll into ‘Simple.dll’ and
change the require statement in your Ruby code into:
require ‘Simple’

···

On Wed, 25 Jun 2003 00:25:28 +0000, Shashank Date wrote:


Simon Strandgaard

“Laurent Sansonetti” laurent@datarescue.be wrote in message

Since your module meant to be named ‘simple’, you should use this line
instead:

void Init_simple()

That didn’t work either … I tried all variations of name capitalizations,
to no vail.

Hey,

···

On Wed, 2003-06-25 at 15:21, Shashank Date wrote:>

void Init_simple()

That didn’t work either … I tried all variations of name capitalizations,
to no vail.

I tried the code on MacOSX and it worked perfectly.

Unfortunately, I don’t have any Windoze box here to try :frowning:


Laurent

“Laurent Sansonetti” laurent@datarescue.be wrote in message

Unfortunately, I don’t have any Windoze box here to try :frowning:

And hence it would be impossible for you to correct my mistakes,
(unless you had super-natural powers ;-))

I was assuming that the IDE would “Do the right thing” … but it has
a mind of its own. Finally, I followed the excellent instructions by
Jon Israelson in [ruby-talk: 30004] and compiled from the DOS
prompt. That worked.

Thank you very much for you time and attention.
– shanko

P.S> I would still like to get it working from within the IDE, so
any body who has an appetite for comparing two large Makefiles
and finding out why they differ in functionality, can contact me offline.
I will be glad to provide the necessary inputs.