More fun with C

I really wasn't trying to complain, I just wanted a little help, and I guess I got carried away since I was frustrated and confused. Reading it over again I realize that I sounded stupid. But you people seem to have a lot of free time to pick apart everything that I say. Sorry for wasting it.

Back to the point: can anyone tell me how to use rb_require or rb_const_get? I get errors when I use them like so: rb_require("myfile"); rb_const_get(rb_cObject, rb_intern("MyClass")). Is there another way to load a file or get a user-defined class name? Thank you for any helpful responses.

Eric Hofreiter just won't give up:

[snip]

···

daz

---------------------------------
Yahoo! FareChase - Search multiple travel sites in one click.

Back to the point: can anyone tell me how to use rb_require or rb_const_get?

I get errors when I use them like so: rb_require("myfile");

rb_const_get(rb_cObject, rb_intern("MyClass")).

Hi, i don't know what you want to do but i think what you want is
rb_path2class ("MyClass");

  Is there another way to load a file

never did it, sorry.

···

On So, 2005-11-13 at 18:16 +0900, Eric Hofreiter wrote:

or get a user-defined class name? Thank you for any helpful responses.

* rb_require("myfile") is exactly same as Kernel.require "myfile".
* rb_const_get(rb_cObject, rb_intern("MyClass")) is exactly same as
Object.const_get("MyClass").

$ cat extconf.rb
require 'mkmf'
create_makefile 'example'

$ cat example.c
#include "ruby.h"

VALUE
example_get_class(VALUE self, VALUE str)
{
    return rb_const_get(rb_cObject, rb_intern(RSTRING(str)->ptr));
}

void
Init_example(void)
{
    rb_require("set");
    rb_define_global_function("get_class", example_get_class, 1);
}

$ make
i686-pc-linux-gnu-gcc -fPIC -O2 -march=athlon-xp -pipe -fPIC -I.
-I/usr/lib/ruby/1.8/i686-linux -I/usr/lib/ruby/1.8/i686-linux -I. -c
example.c
i686-pc-linux-gnu-gcc -shared -L'/usr/lib' -Wl,-R'/usr/lib' -o
example.so example.o -Wl,-R -Wl,/usr/lib -L/usr/lib -L. -lruby18
-ldl -lcrypt -lm -lc

$ irb -f
irb(main):001:0> require 'example' #=> 'set' loaded
=> true
irb(main):002:0> Set == Object.const_get("Set")
=> true
irb(main):003:0> Set == get_class("Set")
=> true

···

On 11/13/05, Eric Hofreiter <erichof425@yahoo.com> wrote:

I really wasn't trying to complain, I just wanted a little help, and I guess I got carried away since I was frustrated and confused. Reading it over again I realize that I sounded stupid. But you people seem to have a lot of free time to pick apart everything that I say. Sorry for wasting it.

Back to the point: can anyone tell me how to use rb_require or rb_const_get? I get errors when I use them like so: rb_require("myfile"); rb_const_get(rb_cObject, rb_intern("MyClass")). Is there another way to load a file or get a user-defined class name? Thank you for any helpful responses.

>Eric Hofreiter just won't give up:
[snip]
>daz

---------------------------------
Yahoo! FareChase - Search multiple travel sites in one click.

--
http://nohmad.sub-port.net

Eric Hofreiter wrote:

I really wasn't trying to complain, I just wanted a little help, and I guess I got carried away since I was frustrated and confused. Reading it over again I realize that I sounded stupid. But you people seem to have a lot of free time to pick apart everything that I say. Sorry for wasting it.
Back to the point: can anyone tell me how to use rb_require or rb_const_get? I get errors when I use them like so: rb_require("myfile"); rb_const_get(rb_cObject, rb_intern("MyClass")). Is there another way to load a file or get a user-defined class name? Thank you for any helpful responses.

Here's a snippet from RMagick. This is the C equivalent of

require 'observer'
class Pixel
    include Observable
    ...
end

rb_require("observer");
observable = rb_const_get(rb_cObject, rb_intern("Observable"));
rb_include_module(Class_Pixel, observable);

Gyoung-Yoon Noh <nohmad@gmail.com> writes:

* rb_require("myfile") is exactly same as Kernel.require "myfile".

If the latter wasn't redefined (think rubygems).

···

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org