# How to export classes from C extensions to Ruby?

**URL:** <https://www.ruby-forum.com/t/how-to-export-classes-from-c-extensions-to-ruby/178188>\
**Category:** Ruby\
**Created:** [October 30, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/how-to-export-classes-from-c-extensions-to-ruby/178188 "2009-10-30T13:26:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 30, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/how-to-export-classes-from-c-extensions-to-ruby/178188/1 "2009-10-30T13:26:04Z")

</div>

Hi, I’ve coded a Ruby C extension in which some exception classes are  
created. However, those classes are not visible in Ruby itself.

This is, if for example I do a “irb”:

irb\> require “myexten.so”

Then the exception classes don’t exist. But the functions in the C  
extension can make use of them, raise them and so.

I create the classes as follows:

void Init\_utils() {  
…  
// Create a exception XDMSURLParsingError.  
class\_error = rb\_define\_class(“MyProjectError”, rb\_eStandardError);  
class\_url\_parsing\_error = rb\_define\_class(“URLParsingError”,  
class\_error);  
}

Should I do something special to export these classes to Ruby? Of  
course a workaround would be creating these classes also in Ruby, but  
seems a bit “dirty” 🙂

Thanks.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 30, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/how-to-export-classes-from-c-extensions-to-ruby/178188/2 "2009-10-30T13:41:17Z")

</div>

2009/10/30 IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> void Init\_utils() {  
> Â …  
> Â // Create a exception XDMSURLParsingError.  
> Â class\_error = rb\_define\_class(“MyProjectError”, rb\_eStandardError);  
> Â class\_url\_parsing\_error = rb\_define\_class(“URLParsingError”, class\_error);  
> }
> 
> Should I do something special to export these classes to Ruby? Of  
> course a workaround would be creating these classes also in Ruby, but  
> seems a bit “dirty” 🙂

Sorry, my fault. The class is visible but it’s not in the module I’ve  
created so I ddin’t capture the correct exception.
