Using gems from C extensions

A

Andre Nathan

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
 
R

Rados³aw Bu³at

TWF5YmUgdGhlIGJldHRlciB3YXkgaXMganVzdCB1c2Ugb25seSByYl9yZXF1aXJlKCJqc29uIikg
aW4gQwpleHRlbnNpb24uIEJlZm9yZSBsb2FkaW5nIGV4dGVuc2lvbiwgaW4gcnVieSBjb2RlIHVz
ZXIgc2hvdWxkIG1hbnVhbGx5CmRvIHJlcXVpcmUgInJ1YnlnZW1zIi4gSSB0aGluayBpdCdzIGJl
dHRlciB3YXkgYmVjYXVzZSBpdCBkb2Vzbid0CmFzc3VtZSB0aGF0IHVzZXIgaGFzIHJ1YnlnZW1z
IGluc3RhbGxlZC4gSW4gcnVieTEuOSB0aGVyZSBpcyBubwpwcm9ibGVtIGJlY2F1c2UgcnVieWdl
bXMgaXMgYnVpbHQtaW4gaW50byBjb3JlLgotLSAKUmFkb3OzYXcgQnWzYXQKCmh0dHA6Ly9yYWRh
cmVrLmpvZ2dlci5wbCAtIG3zaiBibG9nCg==
 
A

Andre Nathan

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
 
E

Eric Hodel

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.
 
A

Andre Nathan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top