Using gems from C extensions

Hello

I'm trying to use the json gem from a C extension. Doing this fails with
"no such file to load -- json (LoadError)":

    rb_require("rubygems");
    rb_require("json");

However, if I add

    rb_funcall(rb_mKernel, rb_intern("gem"), 1, rb_str_new2("json"));

before rb_require("json"), then it works fine. Is that how it's supposed
to work?

Thanks,
Andre

Maybe the better way is just use only rb_require("json") in C
extension. Before loading extension, in ruby code user should manually
do require "rubygems". I think it's better way because it doesn't
assume that user has rubygems installed. In ruby1.9 there is no
problem because rubygems is built-in into core.

···

--
Radosław Bułat

http://radarek.jogger.pl - mój blog

Yes. rb_require calls the base require, not the RubyGems overridden one.

Instead of calling #gem, you can rb_funcall require instead.

···

On Jan 30, 2008, at 03:40 AM, Andre Nathan wrote:

I'm trying to use the json gem from a C extension. Doing this fails with
"no such file to load -- json (LoadError)":

   rb_require("rubygems");
   rb_require("json");

However, if I add

   rb_funcall(rb_mKernel, rb_intern("gem"), 1, rb_str_new2("json"));

before rb_require("json"), then it works fine. Is that how it's supposed
to work?

This is for a controlled environment. I know I have rubygems :slight_smile:

I just wanted to understand why the explicit call to "gem" is needed,
since in ruby code it isn't.

Best,
Andre

···

On Thu, 2008-01-31 at 05:33 +0900, Radosław Bułat wrote:

Maybe the better way is just use only rb_require("json") in C
extension. Before loading extension, in ruby code user should manually
do require "rubygems". I think it's better way because it doesn't
assume that user has rubygems installed. In ruby1.9 there is no
problem because rubygems is built-in into core.

Strange, I tried that but it didn't find the json gem. I tried with both

   rb_require("rubygems");
   rb_funcall(rb_mKernel, rb_intern("require"), 1, rb_str_new2("json"));

and

   rb_funcall(rb_mKernel, rb_intern("require"), 1,
rb_str_new2("rubygems"));
   rb_funcall(rb_mKernel, rb_intern("require"), 1, rb_str_new2("json"));

I'm using ruby 1.8.6 (2007-09-23 patchlevel 110).

Andre

···

On Thu, 2008-01-31 at 08:14 +0900, Eric Hodel wrote:

Instead of calling #gem, you can rb_funcall require instead.