# Another beginner question

**URL:** <https://www.ruby-forum.com/t/another-beginner-question/105978>\
**Category:** Ruby\
**Created:** [June 5, 2007, 7:44am UTC](https://www.ruby-forum.com/t/another-beginner-question/105978 "2007-06-05T07:44:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![micahphone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@micahphone](https://www.ruby-forum.com/u/micahphone)\
**Post date:** [June 5, 2007, 7:44am UTC](https://www.ruby-forum.com/t/another-beginner-question/105978/1 "2007-06-05T07:44:21Z")

</div>

Sorry if my “newbie” questions are excessive. I promise that if/when I  
start getting on my feet with Ruby programming I will contribute back to  
the learning process 10-fold. That’s how I role.

Simple question:

I was turned on to Ruby via Ruby on Rails at the RailsConf here in  
Portland via a friend. I was told there’s a “plugin” (I’m assuming for  
rails) for inline C code. What I’m wondering-- is as an interpreted  
language, if there is a chunk of code that needs to execute very  
quickly, is there a way to have C code that’s compiled? Perhaps a  
library?

So, let’s say I have a little GTK+ GUI that I wrote in Ruby to save  
time, but it has some process-intensive routine-- could I write that  
routine in C and have it as a compiled binary and run within my Ruby  
code?

–

- Micah C.

---

<div class="post-metadata">

**Author:** ![micahphone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@micahphone](https://www.ruby-forum.com/u/micahphone)\
**Post date:** [June 5, 2007, 8:08am UTC](https://www.ruby-forum.com/t/another-beginner-question/105978/2 "2007-06-05T08:08:25Z")

</div>

On 5 Jun 2007, at 14:43, Micah C. wrote:

> execute very quickly, is there a way to have C code that’s  
> compiled? Perhaps a library?
> 
> So, let’s say I have a little GTK+ GUI that I wrote in Ruby to save  
> time, but it has some process-intensive routine-- could I write  
> that routine in C and have it as a compiled binary and run within  
> my Ruby code?

I’ve never used inline C in Ruby, but it is available (it is not  
Rails specific AFAIK). You can also write C code and compile as an  
extension:

[http://www.rubycentral.com/book/ext\_ruby.html](http://www.rubycentral.com/book/ext_ruby.html)

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![micahphone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@micahphone](https://www.ruby-forum.com/u/micahphone)\
**Post date:** [June 5, 2007, 8:29am UTC](https://www.ruby-forum.com/t/another-beginner-question/105978/3 "2007-06-05T08:29:27Z")

</div>

On Jun 4, 2007, at 22:43, Micah C. wrote:

> execute very quickly, is there a way to have C code that’s  
> compiled? Perhaps a library?

There’s a gem called RubyInline that does this (and C++ and Fortran).

> So, let’s say I have a little GTK+ GUI that I wrote in Ruby to save  
> time, but it has some process-intensive routine-- could I write  
> that routine in C and have it as a compiled binary and run within  
> my Ruby code?

RubyInline does all the build steps for you, you just write the C,  
save the file, and run your tests to make sure it works. Just as if  
you’d written regular Ruby.

(Of course, you should have regular ruby, profile it, refactor for  
speed, then write in C when you can’t get any more speed out of Ruby.)

---

<div class="post-metadata">

**Author:** ![micahphone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@micahphone](https://www.ruby-forum.com/u/micahphone)\
**Post date:** [June 5, 2007, 8:37am UTC](https://www.ruby-forum.com/t/another-beginner-question/105978/4 "2007-06-05T08:37:27Z")

</div>

Thank you. I appreciate the feedback.

- Micah C.
