newb: setting up GNU Emacs for Ruby -- help

J

John Gabriele

I'd like to get GNU Emacs set up for use with Ruby, and I'm hoping
someone here might help.

I've got emacs installed, and I found the *.el files in
.../src/ruby-1.8.4/misc, but I think the README in there assumes you
already know what to do with the files therein.

I know some of the basics of using emacs, and used the X11-based GUI
(and also M-x customize-variable) to set up the following ~/.emacs
file so far:

=============== snip ================
(custom-set-variables
;; custom-set-variables was added by Custom -- don't edit or cut/paste it!
;; Your init file should contain only one such instance.
'(case-fold-search t)
'(current-language-environment "Latin-1")
'(default-input-method "latin-1-prefix")
'(global-font-lock-mode t nil (font-lock))
'(indent-tabs-mode nil)
'(tab-width 4)
'(transient-mark-mode t))
(custom-set-faces
;; custom-set-faces was added by Custom -- don't edit or cut/paste it!
;; Your init file should contain only one such instance.
)

(color-theme-gray30)
=============== /snip ================

(Note: I'm also trying to get emacs to use all spaces (no tabs) and
4-space indents, as you might be able to tell from the above ~/.emacs
file contents.)

So, how do I get emacs to recognize Ruby? Am I supposed to copy one or
more of the misc/*.el files to my ~/.emacs.d directory?

Also, are we supposed to use *both* ruby-mode and ruby-electric, or
instead, just one or the other?

Thanks,
---John
 
R

Ryan Davis

So, how do I get emacs to recognize Ruby? Am I supposed to copy one or
more of the misc/*.el files to my ~/.emacs.d directory?

Yes, and you want to add ruby-mode.el to the autoload list:

(autoload 'ruby-mode "ruby-mode" "Mode for editing ruby source files")
(add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
(add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
(autoload 'run-ruby "inf-ruby" "Run an inferior Ruby process")
(autoload 'inf-ruby-keys "inf-ruby" "Set local key defs for inf-ruby
in ruby-mode")

Check out http://wiki.rubygarden.org/Ruby/page/show/
InstallingEmacsExtensions

poke around in there... there is some good stuff.
Also, are we supposed to use *both* ruby-mode and ruby-electric, or
instead, just one or the other?

Yes on ruby-mode, maybe on ruby-electric. Drives me bonkers but you
might like it.
 
J

John Gabriele

Yes, and you want to add ruby-mode.el to the autoload list:

(autoload 'ruby-mode "ruby-mode" "Mode for editing ruby source files")
(add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
(add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))
(autoload 'run-ruby "inf-ruby" "Run an inferior Ruby process")
(autoload 'inf-ruby-keys "inf-ruby" "Set local key defs for inf-ruby
in ruby-mode")

I appended those to my ~/.emacs file, but there's still no syntax
highlighting when I open a Ruby source file.

Also, you mention to add ruby-mode.el to the "autoload list", but you
have three lines above for adding stuff to this autoload list. Are
ruby-mode, run-ruby, and inf-ruby-keys three separate thingies inside
ruby-mode.el?

Thanks. That page says:

;; uncomment the next line if you want syntax highlighting
;;(add-hook 'ruby-mode-hook 'turn-on-font-lock)

but I've already got that (global-font-lock-mode t nil (font-lock)) in
my ~/.emacs file. Do I need that "add-hook" line too?

I've tried appending it to the end of ~/.emacs, but still no syntax
highlighting.

Also, I've read here and there on nntp://gnu.emacs.help that the way
I'm trying to get all spaces (and no tabs at all), with 4-space
indents:

|
| '(indent-tabs-mode nil)
| '(tab-width 4)
|

is "evil". Am I doing it right? So far, tabs seem to be coming out
just the way I want. :)
poke around in there... there is some good stuff.


Yes on ruby-mode, maybe on ruby-electric. Drives me bonkers but you
might like it.

What does ruby-electric do that ruby-mode doesn't? Do they overlap?
What is it about ruby-electric drives you nuts?

Thanks,
---John
 
R

Ryan Davis

I appended those to my ~/.emacs file, but there's still no syntax
highlighting when I open a Ruby source file.

Also, you mention to add ruby-mode.el to the "autoload list", but you
have three lines above for adding stuff to this autoload list. Are
ruby-mode, run-ruby, and inf-ruby-keys three separate thingies inside
ruby-mode.el?

I just copied in some of what I have from my setup... It looks like
you need to start poking around in emacs and learn what it is doing.
Try: "C-h f autoload RET" to start seeing what args autoload takes.
It should be able to answer your question above.
;; uncomment the next line if you want syntax highlighting
;;(add-hook 'ruby-mode-hook 'turn-on-font-lock)

but I've already got that (global-font-lock-mode t nil (font-lock)) in
my ~/.emacs file. Do I need that "add-hook" line too?

I've tried appending it to the end of ~/.emacs, but still no syntax
highlighting.

Also, I've read here and there on nntp://gnu.emacs.help that the way
I'm trying to get all spaces (and no tabs at all), with 4-space
indents:

| '(indent-tabs-mode nil)
| '(tab-width 4)

is "evil". Am I doing it right? So far, tabs seem to be coming out
just the way I want. :)

I've got:

(setq tab-width 4
indent-tabs-mode t

*shrug*

Do note that ruby-mode will use 2 (which is idiomatic) because of:

(defcustom ruby-indent-level 2
"*Indentation of ruby statements."
:type 'integer :group 'ruby)

Stick with 2 and we won't have to kill you. :p
What does ruby-electric do that ruby-mode doesn't? Do they overlap?
What is it about ruby-electric drives you nuts?

Again, I suggest you start looking at some lisp and poking around.
 
J

John Gabriele

On Aug 15, 2006, at 11:07 AM, John Gabriele wrote:

[snip]
Do note that ruby-mode will use 2 (which is idiomatic) because of:

(defcustom ruby-indent-level 2
"*Indentation of ruby statements."
:type 'integer :group 'ruby)

Stick with 2 and we won't have to kill you. :p

Uh oh. I might have to live dangerously then. ;)
Again, I suggest you start looking at some lisp and poking around.

Check.

Thanks,
---John
 
J

John Gabriele

Which OS / distro are you running?

Right now: Debian Etch. At home: Ubuntu 6.06. On servers, usually Sarge.
Many provide a package which
separately er... packages the Ruby elisp files and makes sure they're
in the load path, etc. On Debian (and Ubuntu?) the package is named
ruby1.8-elisp for ruby 1.8.

Thanks. Dunno how I missed that. I'll try it if we can't get this
working using what we have (the files from ruby-1.8.4, and my ~/.emacs
file). Maybe that package comes with some kind of post-inst script
that modifies some system-wide emacs config file... (?)
What the autoload function does is make Emacs load a particular file
(e.g., "ruby-mode") when you user a particular function (e.g.,
'ruby-mode).

Ah. The single-quote mark indicates a function name maybe...
Multiple files generally means multiple autoload
invocations. Ask your local Emacs installation for more information
with C-h f autoload RET!
Ok.

The "ruby-mode.el" file contains the main Ruby editing mode. The file
"ruby-electric.el" contains so-termed "electric" keybindings for the
Ruby editing mode (more on that later). The file "inf-ruby" has code
for interacting with an "inferior" ruby process communicating with
Emacs; i.e., running irb in an Emacs window, sending it code from
other buffers, etc.

Ah. Looks like I should copy the .../src/ruby-1.8.4/misc/inf-ruby.el
to my ~/.emacs.d as well then. I'd only previously copied the one
ruby-mode.el file.
If you have global-font-lock-mode enabled and are in Ruby mode, you
should see syntax highlighting. Do M-x customize-variable

(I hit return after that. Continuing...)
global-font-lock-mode RET to make sure that customization is being
loaded properly.

It says non-nil, so I that should mean that Global-Font-Lock mode is enabled.
If so, when you open a file ending in '.rb' do you
see "Ruby" in the modeline?

No. It says "Fundamental".
If you don't see it, then your
auto-mode-alist addition isn't getting run. Can you manually switch
to ruby-mode with M-x ruby-mode RET?

No. I get "[No Match]". Can't autocomplete it either. I bet I need to
copy that inf_ruby.el file into ~/.emacs.d...

Nope. "File mode specification error: (file-error "Cannot open load
file" "ruby-mode")". Note, it said that without inf_ruby.el in place
as well -- I just hadn't noticed.

Maybe I'm supposed to copy these .el files to some subdirectory
*beneath* ~/.emacs.d?
If not, then the ruby elisp code
isn't being made available to your Emacs instance.


Because (1) some tools like 'make' and really old versions of 'sh'
consider tabs vs. spaces significant, (2a) you can stomp on the toes
of people who do use tabs when editing their files, and (2b) you can
create extraneous diffs when editing files which do contain tabs.
Emacs largely avoid (1) for you automatically, and as for (2) I think
people who use tabs deserve to be stomped on ;-).

I'll take that as a "yes" that my settings are suitable for what I'm after. :)
[snip notes on ruby-electric]

Anyway, hope this helps!

Yes, it does! Thank you. When we're done here, I'll try to condense
this tread into something suitable for the wiki.

---John
 
J

Julian I. Kamil

Here is what I have in my .emacs:

=====
(setq load-path (append load-path '("/path/to/ruby.el")))

(cond (window-system
(require 'font-lock)
(setq font-lock-support-mode 'lazy-lock-mode)
(setq font-lock-maximum-decoration t)
(global-font-lock-mode t)
))

(autoload 'ruby-mode "ruby-mode" "Load ruby-mode")
(add-to-list 'auto-mode-alist '("\\.rb\\'" . ruby-mode))

(add-hook 'ruby-mode-hook
(function
(lambda ()
(font-lock-mode 1)
)
)
)

(set-variable 'ruby-indent-level 4)
=====

I hope that helps...

Julian I. Kamil <[email protected]>
 
J

John Gabriele

[snip]
Which OS / distro are you running? Many provide a package which
separately er... packages the Ruby elisp files and makes sure they're
in the load path, etc. On Debian (and Ubuntu?) the package is named
ruby1.8-elisp for ruby 1.8.

For those interested...

At home (on Ubuntu), I installed the ruby-elisp package (which depends
on ruby1.8-elisp). It only depends on having emacs installed (not
Ruby). Incidentally, I installed emacs via apt-get, and I've got Ruby
installed from source in /opt.

Anyhow, during the install it says:

Setting up ruby1.8-elisp (1.8.4-1ubuntu1.1) ...
install/ruby1.8-elisp: Handling install for emacsen flavor emacs21
Wrote /usr/share/emacs21/site-lisp/ruby1.8-elisp/inf-ruby.elc
Wrote /usr/share/emacs21/site-lisp/ruby1.8-elisp/rubydb2x.elc
Wrote /usr/share/emacs21/site-lisp/ruby1.8-elisp/rubydb3x.elc
Wrote /usr/share/emacs21/site-lisp/ruby1.8-elisp/ruby-electric.elc
Wrote /usr/share/emacs21/site-lisp/ruby1.8-elisp/ruby-mode.elc
Done

These files seem to be generated during the install. Their source
counterparts are:

/usr/share/emacs/site-lisp/ruby1.8-elisp/inf-ruby.el
/usr/share/emacs/site-lisp/ruby1.8-elisp/ruby-electric.el
/usr/share/emacs/site-lisp/ruby1.8-elisp/ruby-mode.el
/usr/share/emacs/site-lisp/ruby1.8-elisp/rubydb2x.el
/usr/share/emacs/site-lisp/ruby1.8-elisp/rubydb3x.el

This package also provides /etc/emacs/site-start.d/50ruby1.8-elisp.el
which contains a few informative comments, and does things involving
load-path and autoload, which I don't yet understand.

Now when I open up a Ruby file, it's syntax highlighted. Strangely (to
me), when I write any Ruby, even though that ruby-electric module (is
that the correct term?) is present, I don't get the "electric"
behaviour that Marshall described. Any idea how one might
enable/disable it?

During the install, my ~/.emacs file wasn't touched.

The last issue was that it used, by default, 2-space indents. I
"fixed" that :), and put these notes and others on the wiki:
http://wiki.rubygarden.org/Ruby/page/show/EmacsWithRuby

At least, now, I can gradually learn more about emacs while still
getting my work done. I may try a similar procedure tomorrow on the
Debian Etch machine unless the other tips in this thread end up
working out (ex. Julian's snippet below) for doing it manually.

Thanks,
---John
 
J

John Gabriele

Here is what I have in my .emacs:

=====
(setq load-path (append load-path '("/path/to/ruby.el")))

(cond (window-system
(require 'font-lock)
(setq font-lock-support-mode 'lazy-lock-mode)
(setq font-lock-maximum-decoration t)
(global-font-lock-mode t)
))

(autoload 'ruby-mode "ruby-mode" "Load ruby-mode")
(add-to-list 'auto-mode-alist '("\\.rb\\'" . ruby-mode))

(add-hook 'ruby-mode-hook
(function
(lambda ()
(font-lock-mode 1)
)
)
)

(set-variable 'ruby-indent-level 4)
=====

I hope that helps...

Thanks Julian.

A major key seems to be adding to the load-path. I've added a bit on
that to http://wiki.rubygarden.org/Ruby/page/show/InstallingEmacsExtensions
, but have seen a couple different ways to do it besides yours, and so
have put all three up there.

Hm. I see that you set ruby-indent-level right there in your .emacs
file rather than modifying the ruby-mode.el file.

---John
 
K

Kristof Bastiaensen

On Aug 15, 2006, at 11:07 AM, John Gabriele wrote:

[snip]
Do note that ruby-mode will use 2 (which is idiomatic) because of:

(defcustom ruby-indent-level 2
"*Indentation of ruby statements."
:type 'integer :group 'ruby)

Stick with 2 and we won't have to kill you. :p

Uh oh. I might have to live dangerously then. ;)

I personally find 2 too narrow to distinguish different indentation
levels, so I use 3.

If I may shamelessly suggest my own extension: ri-emacs
<http://rubyforge.org/projects/ri-emacs/>
It's an interface for ri from emacs that uses completion.

Kristof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top