# Ruby to C?

**URL:** <https://www.ruby-forum.com/t/ruby-to-c/75308>\
**Category:** Ruby\
**Created:** [November 9, 2006, 12:37am UTC](https://www.ruby-forum.com/t/ruby-to-c/75308 "2006-11-09T00:37:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [November 9, 2006, 12:37am UTC](https://www.ruby-forum.com/t/ruby-to-c/75308/1 "2006-11-09T00:37:11Z")

</div>

Okay, I’m working an a robotics project and instead of using C could I  
actually write it in ruby then convert it into C code?

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [November 9, 2006, 8:11am UTC](https://www.ruby-forum.com/t/ruby-to-c/75308/2 "2006-11-09T08:11:42Z")

</div>

Mohammad — wrote:

> Okay, I’m working an a robotics project and instead of using C could I  
> actually write it in ruby then convert it into C code?

If you mean convert automatically, no, not really. Not unless what you  
write  
in Ruby is very generic, and you don’t use any of C’s distinct  
properties.  
If that were true, converting to C would have little point.

Why do you think you need to convert to C? For speed? I have written a  
number of projects in Ruby because the development time is so short,  
then,  
after establishing the design, I converted to a faster language. But the  
conversion was by no means easy.

So in essence I took advantage of the prototyping speed of Ruby, then I  
went  
after the execution speed of another language. But I didn’t seriously  
expect to be able to automatically translate between languages.

Here is an example of a physics simulation program I wrote in Ruby, then  
converted to C++:

> **[\* Modeling Gravity with Ruby](https://arachnoid.com/ruby/gravity/index.html)**
>
> Ruby Gravitation. A Ruby program that models gravitation in real-time, as well as an article describing the physics of gravitation and computer modeling.

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [November 9, 2006, 8:57am UTC](https://www.ruby-forum.com/t/ruby-to-c/75308/3 "2006-11-09T08:57:51Z")

</div>

On Nov 9, 2006, at 2:10 AM, Paul L. wrote:

> If that were true, converting to C would have little point.  
> Not quite accurate. Depending on what you’re doing ruby2c might work.  
> Given that it is a robotics program, we’re generally talking fairly  
> boring prorgamming anyhow. It might be translatable with ruby2c,  
> possibly even without much work.

> Why do you think you need to convert to C?

Presumably so he can download the program to the robot and ruby  
doesn’t run there.

> For speed? I have written a  
> number of projects in Ruby because the development time is so  
> short, then,  
> after establishing the design, I converted to a faster language.  
> But the  
> conversion was by no means easy.

There are several much easier ways to make your ruby fast. Usually  
keeping most everything ruby. Full ports are my absolute LAST option.

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [November 19, 2006, 7:44am UTC](https://www.ruby-forum.com/t/ruby-to-c/75308/4 "2006-11-19T07:44:12Z")

</div>

I’ve had very good success with Dominik’s ruby2c compiler:

[http://rubyforge.org/projects/ruby2cext/](http://rubyforge.org/projects/ruby2cext/)

I use his CVS version and can get 1-5X speedups. It handles most ruby  
code  
you throw at it and maintains the full semantics.

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [November 9, 2006, 9:55am UTC](https://www.ruby-forum.com/t/ruby-to-c/75308/5 "2006-11-09T09:55:57Z")

</div>

> > Why do you think you need to convert to C?
> 
> Presumably so he can download the program to the robot and ruby  
> doesn’t run there.

Mohammad, are you downloading the program to a hardware robot or are you  
simply doing complex matrix operations for robot simulations(which can  
be  
done in any language but certain educational institutes insist on C).

Regards,

- vihan
