简体   繁体   中英

Can't use Gems in Ruby C Extension unless i use full path

Whenever I try to use a gem using Ruby's C API (with rb_require), I get a "cannot load such file --" error UNLESS i use the full path. Requiring gems work fine in regular ruby scripts. Is these something else I need to do? These gems are installed using Bundler.

When you call rb_require from an extension you are calling the core “built-in” process for requiring files in Ruby. This bypasses the Gem handling code that is added by Rubygems when it replaces the Kernel#require method.

In order to get the proper gem handling you need to call the normal require method and let the usual method dispatch route the call to the Rubygems code. You can do that with:

rb_funcall(rb_cObject, rb_intern("require"), 1, rb_str_new_cstr("the_gem"));

You could call rb_intern once and store the result to avoid calling it multiple times if you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM