[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using gems from C extensions

Andre Nathan

1/30/2008 11:41:00 AM

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


4 Answers

Radoslaw Bulat

1/30/2008 8:33:00 PM

0

TWF5YmUgdGhlIGJldHRlciB3YXkgaXMganVzdCB1c2Ugb25seSByYl9yZXF1aXJlKCJqc29uIikg
aW4gQwpleHRlbnNpb24uIEJlZm9yZSBsb2FkaW5nIGV4dGVuc2lvbiwgaW4gcnVieSBjb2RlIHVz
ZXIgc2hvdWxkIG1hbnVhbGx5CmRvIHJlcXVpcmUgInJ1YnlnZW1zIi4gSSB0aGluayBpdCdzIGJl
dHRlciB3YXkgYmVjYXVzZSBpdCBkb2Vzbid0CmFzc3VtZSB0aGF0IHVzZXIgaGFzIHJ1YnlnZW1z
IGluc3RhbGxlZC4gSW4gcnVieTEuOSB0aGVyZSBpcyBubwpwcm9ibGVtIGJlY2F1c2UgcnVieWdl
bXMgaXMgYnVpbHQtaW4gaW50byBjb3JlLgotLSAKUmFkb3OzYXcgQnWzYXQKCmh0dHA6Ly9yYWRh
cmVrLmpvZ2dlci5wbCAtIG3zaiBibG9nCg==

Andre Nathan

1/30/2008 8:38:00 PM

0

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.

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

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

Best,
Andre


Eric Hodel

1/30/2008 11:14:00 PM

0

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?

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

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

Andre Nathan

1/31/2008 1:10:00 AM

0

On Thu, 2008-01-31 at 08:14 +0900, Eric Hodel wrote:
> Instead of calling #gem, you can rb_funcall require instead.

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