# Using gems from C extensions

**URL:** <https://rubytalk.org/t/using-gems-from-c-extensions/44125>\
**Category:** ruby-talk\
**Created:** [30 January 2008 11:40 UTC](https://rubytalk.org/t/using-gems-from-c-extensions/44125 "2008-01-30T11:40:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andre\_Nathan](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Andre\_Nathan](https://rubytalk.org/u/Andre_Nathan)\
**Post date:** [30 January 2008 11:40 UTC](https://rubytalk.org/t/using-gems-from-c-extensions/44125/1 "2008-01-30T11:40:35Z")

</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)":

&nbsp;&nbsp;&nbsp;&nbsp;rb\_require("rubygems");  
&nbsp;&nbsp;&nbsp;&nbsp;rb\_require("json");

However, if I add

&nbsp;&nbsp;&nbsp;&nbsp;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:** ![Radoslaw\_Bulat](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Radoslaw\_Bulat](https://rubytalk.org/u/Radoslaw_Bulat)\
**Post date:** [30 January 2008 20:33 UTC](https://rubytalk.org/t/using-gems-from-c-extensions/44125/2 "2008-01-30T20:33:28Z")

</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:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [30 January 2008 23:14 UTC](https://rubytalk.org/t/using-gems-from-c-extensions/44125/3 "2008-01-30T23:14:11Z")

</div>

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)":
> > 
> > &nbsp;&nbsp;&nbsp;rb\_require("rubygems");  
> > &nbsp;&nbsp;&nbsp;rb\_require("json");
> > 
> > However, if I add
> > 
> > &nbsp;&nbsp;&nbsp;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?

---

<div class="post-metadata">

**Author:** ![Andre\_Nathan](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Andre\_Nathan](https://rubytalk.org/u/Andre_Nathan)\
**Post date:** [30 January 2008 20:38 UTC](https://rubytalk.org/t/using-gems-from-c-extensions/44125/4 "2008-01-30T20:38:25Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Andre\_Nathan](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Andre\_Nathan](https://rubytalk.org/u/Andre_Nathan)\
**Post date:** [31 January 2008 01:09 UTC](https://rubytalk.org/t/using-gems-from-c-extensions/44125/5 "2008-01-31T01:09:33Z")

</div>

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

&nbsp;&nbsp;&nbsp;rb\_require("rubygems");  
&nbsp;&nbsp;&nbsp;rb\_funcall(rb\_mKernel, rb\_intern("require"), 1, rb\_str\_new2("json"));

and

&nbsp;&nbsp;&nbsp;rb\_funcall(rb\_mKernel, rb\_intern("require"), 1,  
rb\_str\_new2("rubygems"));  
&nbsp;&nbsp;&nbsp;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.
