From: Gyoung-Yoon Noh Date: 2005-11-13T19:01:31+09:00 Subject: Re: More fun with C On 11/13/05, 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. > > >Eric Hofreiter just won't give up: > [snip] > >daz > > > --------------------------------- > Yahoo! FareChase - Search multiple travel sites in one click. > * 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 -- http://nohmad.sub-port.net