Ruby mode for Emacs

Emacs is not distributed with a mode for editing Ruby programs, but such a mode can be found in the source code distribution of Ruby. It has the usual features for Emacs language modes such as syntax highlighting and indentation support. It also includes some support for the Ruby debugger, and for running a Ruby interpreter in an Emacs buffer.

Installation

You can get the elisp files implementing the Ruby mode by downloading a .tar.gz file with the Ruby source code distribution from the ruby web site. In this .tar.gz file you will find some elisp files in the directory named "misc", these files implement the Ruby mode for Emacs.

In order to install the Ruby mode, copy the files to wherever you store your elisp files, and insert something like the code below in your .emacs file

(add-to-list 'load-path "where you stored the elisp files")
(add-to-list 'auto-mode-alist '("\\.rb\\'" . ruby-mode))
(autoload 'ruby-mode "ruby-mode" "Major mode for editing Ruby code" t)

Where you should replace the red text with the path to where you stored the elisp files.

This configures Emacs to be able to find the Ruby mode elisp files, and to enter the Ruby mode whenever a file named *.rb is opened.

Automatic indentation

If you want to have each new line you enter indented automatically insert the following code in your .emacs file:

(add-hook 'ruby-mode-hook (lambda () (local-set-key "\r" 'newline-and-indent)))

This binds the return key to the function newline-and-indent whenever Ruby mode is entered.

Running Ruby inside Emacs

If you want to run an interactive ruby interpreter inside Emacs, which can be very handy for quick experiments, insert the following in your .emacs file

(require 'inf-ruby)

This gives access to a function called run-ruby, which runs IRB in an Emacs buffer.

Electric commands

The Ruby mode features some so called "electric" commands, which can insert some text automatically. The texts that can be inserted by this features include:

I have some mixed feelings about this feature. Sometimes it makes me work faster, and sometimes it is just an annoyance. If you want to try it out, enable it by inserting the following in your .emacs file.

(require 'ruby-electric)
(add-hook 'ruby-mode-hook (lambda () (ruby-electric-mode t)))

This enables the ruby-electric mode whenever ruby mode is entered.

Debugger support

I haven't tried using the Ruby debugger within Emacs yet, so I can't say anything about it. If I ever get around to trying it, I will update this page.

peter.peca.dk About Emacs Free Software Projects Linux

Valid XHTML 1.0 Strict