Using Emacs for Ruby development

Emacs, the God of all things. I’ve been working my way through, over the past several years (after having been introduced to it by Ravi), all kinds of books and tutorials on lisp. Unfortunately, I’ve not been particularly good at following through and becoming a master, so I’m not really a guru or anything when it comes to Emacs skills. (I promise, it will change this year.)

Anyway, while working on Ruby, I was looking to find a good editor. Who isn’t, after all? I’ve used Eclipse with RDT and RadRails, but it wasn’t compelling (that might change with this stuff). After moving to the Mac, however, I was looking to buy TextMate – which I’ve heard a lot about. Before buying it, I thought that since it was “inspired” by Emacs, why not give it a shot? Ultimately, I got comfortable using Emacs itself, so I haven’t really given TextMate a real shot, despite owning a license (thanks MacHeist!).

So. This is my first in a series of posts about using Emacs for Ruby and Rails. The “cool” thing for this time is – inferior mode for Ruby. This provides a SLIME-like interface for Ruby, and allows you to work really interactively. What that means is, while typing in the editor, you can ask fragments of code to be interpreted, change only particular definitions quickly and rerun other code that uses them, pipe all that back and forth between windows, so on and so forth. And at any time, you can switch to the Ruby process (running with Emacs using irb) in use and explore the current environment.

Check it out, its very neat and it even comes with Ruby.

The mysql gem and Intel macs

I tried getting rails up and running on my (fairly) new iMac a few days back, and although I installed mysql gem currectly, the code was unable to connect to the mysql server. In fact, it threw an error –

mysql.c:2015: error: ulong undeclared

It befuddled me for a bit, because when the mysql gem is installed (gem list shows it), it compiles the native extensions – and that had succeeded. Or so I thought. Long story short – there appears to be a problem when trying to compile this gem on Intel Macs – and here’s the fix.

1. Go to gems directory – it will be somewhere under your ruby directory. Edit mysql.c, and add “#define ulong unsigned long” to it.
2. Then do ruby extconf.rb install mysql — –with-mysql-dir=yourmysqldirectory/
3. make
4. make install

This fixed things, and now, mysql can be used satisfactorily.

SQLite with RubyOnRails

That was nightmarish!

All I wanted was SQLite working with Rails. That’s all. And it was quick and easy – on my Ubuntu workstation at home, that is. And then, began 2 hours of gruesome agony – as I tried desperately to get the damn thing to work on my RHEL staging box.

I only have shell access to this machine – so no fancy graphical package manager. If I had a package manager, I would have selected libsqlite0-dev (the missing item) as well as libsqlite3-dev just to be safe, and would have saved myself the afore-mentioned two hour hair-pulling.

Here are the steps to get SQLite working with Rails –

* Download sqlite from http://www.sqlite.org/download.html. Compile and install the usual way. Or just get the compiled versions.
* Install libsqlite-dev (both libsqlite-dev3 AND libsqlite-dev0-> AAARGH!)
* Download, compile and install swig -> http://www.swig.org/download.html
* Install the sqlite3-ruby gem
* Downlad ruby-dbi and follow instructions on http://ruby-dbi.rubyforge.org/

Everything should work now! I tested this with Typo 4.0.3 and it works. Phew!

Look Ma, no Ruby-IDE!

Or why the “lack” of an “IDE” is not a big deal for Ruby programmers.

Don’t get me wrong – I’d love to see an IntelliJ like IDE for Ruby. However, I’ve been working on a (personal) Ruby/Rails project for a while now – and I really haven’t felt that the lack of refactoring support or navigation support was holding me down in any way.

The only thing I can attribute this to is that the design/structure of my application makes the importance of these tools somewhat diminished. I’m just able to design the application in ways that the limited OO capabilities of a language like Java doesn’t allow me to. Meta-programming and features like mixins and real closures increase the expressiveness of code making it possible to break down functionality into extemely small and very clear aspects. This makes the application code-base simply a collection of exremely local peices of functionality.

And that makes all the difference.

Update 1: One aspect of the above is that design doesn’t have to be restricted to a combination of a single-inheritence-based class hierarchy and composition-based delegation for reuse. Think about that.

Update 2: I use Eclipse + RDT (+ RadRails) for editing the code.

Update 3: I’ve switched to using a mac, and I use emacs for all my editing.

IRB tab completion

The ruby IRB comes with tab completion. Here’s how to enable it –

Create a file (if it doesn’t already exist) in your home directory called “.irbrc”. In it, cut and paste the following lines –

IRB.conf[:AUTO_INDENT] = true
IRB.conf[:USE_READLINE] = true
IRB.conf[:LOADMODULES] = [] unless IRB.conf.key?(:LOADMODULES)
unless IRB.conf[:LOAD_MODULES].include?(‘irb/completion’)
IRB.conf[:LOAD_MODULES]