# Creating Ruby objects from C

**URL:** <https://rubytalk.org/t/creating-ruby-objects-from-c/36056>\
**Category:** ruby-talk\
**Created:** [11 March 2007 05:11 UTC](https://rubytalk.org/t/creating-ruby-objects-from-c/36056 "2007-03-11T05:11:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Volkmann](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Mark\_Volkmann](https://rubytalk.org/u/Mark_Volkmann)\
**Post date:** [11 March 2007 05:11 UTC](https://rubytalk.org/t/creating-ruby-objects-from-c/36056/1 "2007-03-11T05:11:20Z")

</div>

The following snippet of C code shows my attempt to create a Ruby string object by invoking the "new" method on the Ruby String class. I know it would be easier to do this using the rb\_str\_new or rb\_str\_new2 functions. However, I'm trying to understand how to create arbitrary Ruby objects from C using the "new" method. Can you spot what's wrong with this code?

&nbsp;&nbsp;&nbsp;ID class\_id = rb\_intern("String");  
&nbsp;&nbsp;&nbsp;VALUE stringClass = rb\_const\_get(rb\_cClass, class\_id); // wrong 1st parameter?

&nbsp;&nbsp;&nbsp;ID method\_id = rb\_intern("new");  
&nbsp;&nbsp;&nbsp;VALUE s1 = rb\_funcall(stringClass, method\_id, 1, "test");  
&nbsp;&nbsp;&nbsp;printf("s1 = %s!\n", RSTRING(s1)-\>ptr);

---

<div class="post-metadata">

**Author:** ![Mark\_Volkmann](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Mark\_Volkmann](https://rubytalk.org/u/Mark_Volkmann)\
**Post date:** [11 March 2007 05:25 UTC](https://rubytalk.org/t/creating-ruby-objects-from-c/36056/2 "2007-03-11T05:25:05Z")

</div>

I think I found the answer. Using rb\_class\_new\_instance works and is much simpler that what I was attempting.

> **···**
>
> On Mar 10, 2007, at 11:11 PM, Mark Volkmann wrote:
> 
> > The following snippet of C code shows my attempt to create a Ruby string object by invoking the "new" method on the Ruby String class. I know it would be easier to do this using the rb\_str\_new or rb\_str\_new2 functions. However, I'm trying to understand how to create arbitrary Ruby objects from C using the "new" method. Can you spot what's wrong with this code?
> > 
> > &nbsp;&nbsp;ID class\_id = rb\_intern("String");  
> > &nbsp;&nbsp;VALUE stringClass = rb\_const\_get(rb\_cClass, class\_id); // wrong 1st parameter?
> > 
> > &nbsp;&nbsp;ID method\_id = rb\_intern("new");  
> > &nbsp;&nbsp;VALUE s1 = rb\_funcall(stringClass, method\_id, 1, "test");  
> > &nbsp;&nbsp;printf("s1 = %s!\n", RSTRING(s1)-\>ptr);

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [11 March 2007 20:37 UTC](https://rubytalk.org/t/creating-ruby-objects-from-c/36056/3 "2007-03-11T20:37:04Z")

</div>

require 'rubygems'  
require 'inline'

class X  
&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "blah"  
&nbsp;&nbsp;&nbsp;end  
end

class Y  
&nbsp;&nbsp;&nbsp;inline do |builder|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;builder.c '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUE blah() {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return rb\_funcall(rb\_path2class("X"), rb\_intern("new"), 0);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}'  
&nbsp;&nbsp;&nbsp;end  
end

p Y.new.blah

# % ./blah.rb  
# blah  
# #\<X:0x1090ec4\>

> **···**
>
> On Mar 10, 2007, at 9:11 PM, Mark Volkmann wrote:
> 
> > The following snippet of C code shows my attempt to create a Ruby string object by invoking the "new" method on the Ruby String class. I know it would be easier to do this using the rb\_str\_new or rb\_str\_new2 functions. However, I'm trying to understand how to create arbitrary Ruby objects from C using the "new" method. Can you spot what's wrong with this code?
