4 ms·
C for Rubyists
- jewel 12y agoBeing 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 12y agoThank 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/
- wasd 12y agoThis 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 12y agoThanks 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 12y agoIn fact, such a book has been written before already: http://patshaughnessy.net/ruby-under-a-microscope http://patshaughnessy.net/ruby-under-a-microscope
- timr 12y agoThat's a great book -- highly recommended. But there's plenty more ground to cover!
- pasbesoin 12y agoThank you. I just found that that title is included in O'Reilly's Safari.
- dmunoz 12y agoThis 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-lexer/ http://www.halogenandtoast.com/reconstructing-ruby-our-first...
- theseoafs 12y agoI 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 12y agothere are tons of general purpose c tutorials out there; this one is specifically for rubyists looking to learn how to write extensions in c
- agumonkey 12y agoVery pretty C code. Rarely I find myself thinking 'C is beautiful'
- skylan_q 12y agoI 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 12y agoIf I was able to write that kind of C code, I'd never feel the need to use ruby, python maybe even lisp :)
- pjc50 12y agoIn what way is it beautiful? I had a look and found it to be covered in nasty #ifdefs.
- yoklov 12y agoAs someone who has written C professionally in the past, I agree. IMO this is fairly ugly C. Definitely can't see why someone would consider it to be beautiful. That said, there are some neat things I've seen in the ruby C code in the past. I thought [1] was pretty clever (this pattern shows up several times in the ruby source) the first time I saw it, since it lets you access the members of the union like `foo->as.thing`, which is fairly pretty IMO. [1] https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L761-L771 https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#...
- TickleSteve 12y agoSorry, I have to disagree with you there, as someone with decades of C and c++ experience, this is very dense, averagely-tidy, but using some very-definitely-bad-practice macros (See the definition of "SHOW"). C and C++ can look a hell of a lot better than this....
- jimrhoskins 12y agoStrange, 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 12y agoYes, 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 12y agoGlad you finished all your high priority tasks and made it to working around extensions doing funky things ;-)
- readerrrr 12y agoI can only see the code, without blocking anything. Should I sign in or something?
- zrkzrk 12y agoGreat 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
- mlangdon 12y agoCan anyone point to something parallel for Python? Python-experienced C-neophyte feeling envious over here.
- ayrx 12y agoDo 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 12y agoI'd be pretty interested in the former, but C isn't my forte. :(
- mlangdon 12y agoI was thinking the former, but the latter is also of interest. Thanks!
- lacker 12y agoThis 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 12y agoWe'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.