# Using gems from C extensions

**URL:** <https://www.ruby-forum.com/t/using-gems-from-c-extensions/127564>\
**Category:** Ruby\
**Created:** [January 30, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/using-gems-from-c-extensions/127564 "2008-01-30T12:41:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![reflex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@reflex](https://www.ruby-forum.com/u/reflex)\
**Post date:** [January 30, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/using-gems-from-c-extensions/127564/1 "2008-01-30T12:41:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reflex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@reflex](https://www.ruby-forum.com/u/reflex)\
**Post date:** [January 30, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/using-gems-from-c-extensions/127564/2 "2008-01-30T21:34:54Z")

</div>

## 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](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![reflex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@reflex](https://www.ruby-forum.com/u/reflex)\
**Post date:** [January 30, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/using-gems-from-c-extensions/127564/3 "2008-01-30T21:39:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reflex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@reflex](https://www.ruby-forum.com/u/reflex)\
**Post date:** [January 31, 2008, 2:10am UTC](https://www.ruby-forum.com/t/using-gems-from-c-extensions/127564/4 "2008-01-31T02:10:03Z")

</div>

On Thu, 2008-01-31 at 08:14 +0900, Eric H. 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

---

<div class="post-metadata">

**Author:** ![reflex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@reflex](https://www.ruby-forum.com/u/reflex)\
**Post date:** [January 31, 2008, 12:14am UTC](https://www.ruby-forum.com/t/using-gems-from-c-extensions/127564/5 "2008-01-31T00:14:55Z")

</div>

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

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