
C for Rubyists - montanalow
https://www.omniref.com/ruby/2.1.2/files/ruby.c#annotation=4029392&line=1
======
wasd
This is really, really cool. If I could make one recommendation, after
finishing the tutorial it wasn't clear to me what do next. I want to keep
learning more C and about the Ruby source code but it just sort of leaves me
hanging.

~~~
montanalow
Thanks wasd. The next thing you could do, is highlight some C and ask the
community any questions you have. I intend to keep writing the series, since
there's easily enough material there for a book. If you signup for an Omniref
account, you can follow the project and you'll be emailed whenever I post new
content.

~~~
ucarion
In fact, such a book has been written before already:
[http://patshaughnessy.net/ruby-under-a-
microscope](http://patshaughnessy.net/ruby-under-a-microscope)

~~~
dmunoz
This is an awesome book, and I highly recommend it. Just be aware that very
little of the book concerns itself with C. The majority of the book is an
excellent tour of the internals of the Ruby interpreter.

Also of interest might be this series of blog posts currently being written:
Reconstructing Ruby in C [0]

[0] [http://www.halogenandtoast.com/reconstructing-ruby-our-
first...](http://www.halogenandtoast.com/reconstructing-ruby-our-first-lexer/)

------
jewel
Being able to rewrite a small section of ruby code in C is a great way to
speed up some types of manipulations. This has yielded me two magnitudes of
speedup multiple times in my career so far.

Here's an example using RubyInline, which is a gem that handles the
compilation step for you:

[http://stevenjewel.com/2013/11/detecting-headphone-
use/](http://stevenjewel.com/2013/11/detecting-headphone-use/)

~~~
hayksaakian
Thank you for linking that.

I always imagined that ruby and c were worlds apart, and that it took a crazy
level of mastery to make them work together.

That thought led me to this article about embedding rust in ruby

[http://brson.github.io/2013/03/10/embedding-rust-in-
ruby/](http://brson.github.io/2013/03/10/embedding-rust-in-ruby/)

------
zrkzrk
Great format! And Ruby source is good for learning C. I really got into it
with Matz ruby source code.

For anyone intersted how Ruby parser works, I've written small article about
patching ruby syntax: [https://zarkzork.com/extending-
ruby.html](https://zarkzork.com/extending-ruby.html)

------
agumonkey
Very pretty C code. Rarely I find myself thinking 'C is beautiful'

~~~
skylan_q
I thought the same thing. It's like Matz writes C the way he'd like to write
ruby. I also see some smalltalk inspiration in his style.

Very neat.

~~~
agumonkey
If I was able to write that kind of C code, I'd never feel the need to use
ruby, python maybe even lisp :)

------
mlangdon
Can anyone point to something parallel for Python? Python-experienced
C-neophyte feeling envious over here.

~~~
ayrx
Do you want to learn how CPython internals work or do you want to learn how to
bind C code with Python? If it's the latter, I can point you to the excellent
CFFI library[0], the documentation is pretty clear and it's pretty easy to get
going.

[0]:
[http://cffi.readthedocs.org/en/release-0.8/](http://cffi.readthedocs.org/en/release-0.8/)

~~~
manish_gill
I'd be pretty interested in the former, but C isn't my forte. :(

------
jimrhoskins
Strange, with the Disconnect extension enabled, all I saw was the code, and
not the annotation which I think is the meat of this submission.

If you don't see anything, try disabling social-blocking extensions.

~~~
timr
Yes, the disconnect extension has a bug that breaks the site JS. It's on our
to-do list, but working around extensions that are doing funky things has not
been our highest priority.

Update: I've got a fix for this going out now. Let me know if it's still
broken.

~~~
derwiki
Glad you finished all your high priority tasks and made it to working around
extensions doing funky things ;-)

------
lacker
This is pretty neat. Mucking around with ruby-c connections and python-c++
connections via SWIG have been some of the most nightmarish things I've ever
worked on, so it's cool to see effort aimed at making that sort of connection
more comprehensible. Are there other similar things you're thinking of doing
next?

~~~
montanalow
We're planning a C memory management follow up, with a focus on Ruby GC and
its recent improvements. We're also looking for authors to feature who write
similar content, so if you wanted to do a series on a particular chunk of C
you've worked with, that'd be sweet too.

------
theseoafs
I really dislike that this teaches you how to use _the Ruby virtual machine in
C_ rather than just C itself. If you tried to hop from this over to a real,
true C project, you would be totally lost, which isn't what you want from a
tutorial.

~~~
zem
there are tons of general purpose c tutorials out there; this one is
specifically for rubyists looking to learn how to write extensions in c

