# Reading ruby source

**URL:** <https://rubytalk.org/t/reading-ruby-source/6475>\
**Category:** ruby-talk\
**Created:** [20 June 2003 02:31 UTC](https://rubytalk.org/t/reading-ruby-source/6475 "2003-06-20T02:31:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashank\_Date2](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Shashank\_Date2](https://rubytalk.org/u/Shashank_Date2)\
**Post date:** [20 June 2003 02:31 UTC](https://rubytalk.org/t/reading-ruby-source/6475/1 "2003-06-20T02:31:08Z")

</div>

I’m making a serious attempt to read and understand the C-source which  
compiles to ruby.exe (at least the “core” part, if it has been identified as  
minimalist ruby). Is there any source code level documentation somewhere  
which can help me overcome the initial sense of being lost 😉

If not, what do you gurus, who have mastered the terrain, suggest ? Is there  
a “best reading order” to the various C modules? Like where to begin, what  
may be skipped in the first pass, etc.

Incidentally, when I compiled the 1.8.0 preview 2 on Windows (before  
downloading Andy’s one click installer) it generated “minruby.exe” and I  
presume it is the “core” I mentioned earlier. Am I correct ?  
Thanks,  
– shanko

---

<div class="post-metadata">

**Author:** ![daz](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daz](https://rubytalk.org/u/daz)\
**Post date:** [20 June 2003 04:51 UTC](https://rubytalk.org/t/reading-ruby-source/6475/2 "2003-06-20T04:51:59Z")

</div>

If you can accept the recommendation of someone with 10 minutes  
experience, I’d suggest you open up array.c; skip to the bottom  
where there’s a list of ‘rb\_define\_method’ which are instance  
methods for Array; pick one that has a familiar outcome like …

rb\_define\_method(rb\_cArray, “compact”, rb\_ary\_compact, 0);

… search for rb\_ary\_compact, ignore ‘rb\_ary\_compact\_bang’ for  
a moment and you’ll see that pretty soon one calls the other.  
You can see the whole thing in a few lines. You’re not  
debugging, so you can trust that whatever it calls is correct 🙂

The chapter ‘Extending Ruby’ in the pickaxe tells about VALUE  
(which, -incoming slap?- is a Ruby object) and some of the  
INT2NUM etc. macros (What a fine book). Then pick another  
method that you’re more interested in and try to avoid chasing  
depth until you’re bored with the top level.

Good stuff.

Can’t answer your other bits. Whenever I try to compile  
anything on Windows, I get an error message saying:-  
“Go away, you idiot.” even though I’ve raked together every  
Mingw, UNIXtool, Borland free compiler, DevC++ package that  
I can; read about makefiles (yeeuch) and I call myself a  
Systems Programmer. I never used to be _this_ thick :((

Thanks for listening,

daz

> **···**
>
> “Shashank Date” [sdate@everestkc.net](mailto:sdate@everestkc.net) wrote:
> 
> > Is there a “best reading order” to the various C modules?  
> > Like where to begin, what may be skipped in the first pass, etc.

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [20 June 2003 07:07 UTC](https://rubytalk.org/t/reading-ruby-source/6475/3 "2003-06-20T07:07:40Z")

</div>

> I’m making a serious attempt to read and understand the C-source which  
> compiles to ruby.exe (at least the “core” part, if it has been identified as  
> minimalist ruby). Is there any source code level documentation somewhere  
> which can help me overcome the initial sense of being lost 😉

IMHO the source code is quite understandable, the only complicated  
files being regex.c and eval.c (where the actual evaluation of the  
AST happens). For the other files, you always get a good guide at the  
bottom, like

```
rb_define_method(rb_cString, "initialize", rb_str_init, -1);
rb_define_method(rb_cString, "copy_object", rb_str_replace, 1);
rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1);
rb_define_method(rb_cString, "==", rb_str_equal, 1);

```

This makes it extremely easy to quickly locate the source of a method  
belonging to a core built-in class.

> If not, what do you gurus, who have mastered the terrain, suggest ? Is there  
> a “best reading order” to the various C modules? Like where to begin, what  
> may be skipped in the first pass, etc.

I’m not yet at guru level, so take my suggestions w/ a grain of salt  
(OTOH, you might find them more valuable as I’m just a mere mortal and  
know what things are difficult, instead of believing everything is  
trivial 🙂

Ruby Core

- dln.c: wraps dlopen or the equiv. function of your platform, not very  
interesting
- gc.c: quite easy to follow, of interest only if you want to know how  
the GC works internally, but it’s just mark & sweep doing “common  
sense” things so you can safely skip it.
- st.c: a hash table implementation used internally by Ruby, quite  
straightforward
- eval.c: much harder to read as you have to know the node types to  
follow it; several functions are essentially a big switch() statement  
for a node
- parse.y: this can help you see what different node types correspond  
to by having a look at the grammar.
- regex.c: whatever, don’t read it 🙂

some other .c files contain only support code

Built-in classes  
Take the class you like, scroll down to the Init\_xxx() function and  
locate the C function that implements the method you want to study. No  
particular order required.

You (I 🙂 will sometime want to read eval.c, but it’s hard to follow on  
first read.

> Incidentally, when I compiled the 1.8.0 preview 2 on Windows (before  
> downloading Andy’s one click installer) it generated “minruby.exe” and I  
> presume it is the “core” I mentioned earlier. Am I correct ?

IIRC it’s used to process the extconf.rb files in the ext/ subdirectory.

> **···**
>
> On Fri, Jun 20, 2003 at 11:31:08AM +0900, Shashank Date wrote:
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> The documentation is in Japanese. Good luck.  
> – Rich $alz

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard1](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Ara.T.Howard1](https://rubytalk.org/u/Ara.T.Howard1)\
**Post date:** [20 June 2003 13:55 UTC](https://rubytalk.org/t/reading-ruby-source/6475/4 "2003-06-20T13:55:20Z")

</div>

this is incredibly useful info - can we (you, i, somebody) create a wiki page  
with the following format:

sourcea.c: definition  
sourcebc: definition  
…

then people can add to it at will and i could soon become a really valuable  
resource.

perhaps [http://www.rubygarden.org/ruby?RubyCore](http://www.rubygarden.org/ruby?RubyCore)

??

-a

> **···**
>
> On Fri, 20 Jun 2003, Mauricio [iso-8859-1] Fernández wrote:
> 
> > Ruby Core
> > 
> > - dln.c: wraps dlopen or the equiv. function of your platform, not very  
> > interesting
> > - gc.c: quite easy to follow, of interest only if you want to know how  
> > the GC works internally, but it’s just mark & sweep doing “common  
> > sense” things so you can safely skip it.
> > - st.c: a hash table implementation used internally by Ruby, quite  
> > straightforward
> > - eval.c: much harder to read as you have to know the node types to  
> > follow it; several functions are essentially a big switch() statement  
> > for a node
> > - parse.y: this can help you see what different node types correspond  
> > to by having a look at the grammar.
> > - regex.c: whatever, don’t read it 🙂
> 
> # –
> 
> > Ara Howard  
> > NOAA Forecast Systems Laboratory  
> > Information and Technology Services  
> > Data Systems Group  
> > R/FST 325 Broadway  
> > Boulder, CO 80305-3328  
> > Email: [ara.t.howard@noaa.gov](mailto:ara.t.howard@noaa.gov)  
> > Phone: 303-497-7238  
> > Fax: 303-497-7259  
> > ~ \> ruby -e ‘p(%.\x2d\x29..intern)’  
> > ====================================

---

<div class="post-metadata">

**Author:** ![Shashank\_Date2](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Shashank\_Date2](https://rubytalk.org/u/Shashank_Date2)\
**Post date:** [21 June 2003 02:39 UTC](https://rubytalk.org/t/reading-ruby-source/6475/5 "2003-06-21T02:39:45Z")

</div>

“daz” [dooby@d10.karoo.co.uk](mailto:dooby@d10.karoo.co.uk) wrote in message

> If you can accept the recommendation of someone with 10 minutes  
> experience, I’d suggest you open up array.c; skip to the bottom  
> where there’s a list of ‘rb\_define\_method’ which are instance  
> methods for Array; pick one that has a familiar outcome like …
> 
> rb\_define\_method(rb\_cArray, “compact”, rb\_ary\_compact, 0);
> 
> … search for rb\_ary\_compact, ignore ‘rb\_ary\_compact\_bang’ for  
> a moment and you’ll see that pretty soon one calls the other.  
> You can see the whole thing in a few lines.

Yes, I will definitely accept this recommendation because it makes sense.

[snip]

> Whenever I try to compile anything on Windows, I get  
> an error message saying:-  
> “Go away, you idiot.” even though I’ve raked together every  
> Mingw, UNIXtool, Borland free compiler, DevC++ package that

I have used MSVC++ 6.0 and it works fine. No surprise there.

> Thanks for listening,

Thanks for talking.

> daz

– shanko

---

<div class="post-metadata">

**Author:** ![Shashank\_Date2](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Shashank\_Date2](https://rubytalk.org/u/Shashank_Date2)\
**Post date:** [21 June 2003 02:59 UTC](https://rubytalk.org/t/reading-ruby-source/6475/6 "2003-06-21T02:59:58Z")

</div>

“Mauricio Fernández” [batsman.geo@yahoo.com](mailto:batsman.geo@yahoo.com) wrote in message

> IMHO the source code is quite understandable, the only complicated  
> files being regex.c and eval.c (where the actual evaluation of the  
> AST happens).

And eval.c was the first file I tried to read … no wonder I was  
overwhelmed.

[snip]

Well, you have made very good suggestions.

> Ruby Core
> 
> - dln.c: wraps dlopen or the equiv. function of your platform, not very  
> interesting
> - gc.c: quite easy to follow, of interest only if you want to know how  
> the GC works internally, but it’s just mark & sweep doing “common  
> sense” things so you can safely skip it.
> - st.c: a hash table implementation used internally by Ruby, quite  
> straightforward
> - eval.c: much harder to read as you have to know the node types to  
> follow it; several functions are essentially a big switch() statement  
> for a node
> - parse.y: this can help you see what different node types correspond  
> to by having a look at the grammar.
> - regex.c: whatever, don’t read it 🙂

This is simply great !

> Built-in classes  
> Take the class you like, scroll down to the Init\_xxx() function and  
> locate the C function that implements the method you want to study. No  
> particular order required.
> 
> You (I 🙂 will sometime want to read eval.c, but it’s hard to follow on  
> first read.
> 
> > Incidentally, when I compiled the 1.8.0 preview 2 on Windows (before  
> > downloading Andy’s one click installer) it generated “minruby.exe” and I  
> > presume it is the “core” I mentioned earlier. Am I correct ?
> 
> IIRC it’s used to process the extconf.rb files in the ext/ subdirectory.

Thanks very much. This helps a lot !  
– shanko

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [20 June 2003 14:01 UTC](https://rubytalk.org/t/reading-ruby-source/6475/7 "2003-06-20T14:01:07Z")

</div>

I prefer RubyInterpreterSourceCode, quite verbose but precise. And then  
make a hierarchy (because everything in one page could be too big) w/

- /Core
- /BuiltinClasses

I’m adding this to the Wiki unless somebody complains here soon 🙂

> **···**
>
> On Fri, Jun 20, 2003 at 10:55:20PM +0900, ahoward wrote:
> 
> > > Ruby Core
> > > 
> > > - dln.c: wraps dlopen or the equiv. function of your platform, not very  
> > > interesting
> > > - gc.c: quite easy to follow, of interest only if you want to know how  
> > > the GC works internally, but it’s just mark & sweep doing “common  
> > > sense” things so you can safely skip it.
> > > - st.c: a hash table implementation used internally by Ruby, quite  
> > > straightforward
> > > - eval.c: much harder to read as you have to know the node types to  
> > > follow it; several functions are essentially a big switch() statement  
> > > for a node
> > > - parse.y: this can help you see what different node types correspond  
> > > to by having a look at the grammar.
> > > - regex.c: whatever, don’t read it 🙂
> > 
> > this is incredibly useful info - can we (you, i, somebody) create a wiki page  
> > with the following format:
> > 
> > sourcea.c: definition  
> > sourcebc: definition  
> > …
> > 
> > then people can add to it at will and i could soon become a really valuable  
> > resource.
> > 
> > perhaps [http://www.rubygarden.org/ruby?RubyCore](http://www.rubygarden.org/ruby?RubyCore)
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Thinking is dangerous. It leads to ideas.  
> – Seen on #Debian
