Ruby-mode / inferior ruby

Emacs’ ruby-mode doesn’t seem to work too well. I would be most happy if
anyone could tell me if what I see is the normal behaviour, or if there
is an error in my setup. An example, given the following code:

def pow(x)
x * x
end

pow()

When I evaluate this twice using C-c C-e, I get the following in the in-
ferior ruby buffer:

irb(main):001:0> irb(main):002:1> irb(main):003:1> nil
irb(main):004:0> ArgumentError: wrong # of arguments(0 for 1)
from (irb):5:in pow' from (irb):5 irb(main):006:0> irb(main):007:1> irb(main):008:1> nil irb(main):009:0> ArgumentError: wrong # of arguments(0 for 1) from (irb):10:inpow’
from (irb):10
irb(main):011:0>

As you can see, the prompt is echoed for every line evaluated, and the
line numbers are not reset so that the second evaluation reports excep-
tions on wrong lines. This is extremely annoying.

I have tried on the following combinations of software:

emacs 20.7.2 / 21.2.1
irb 0.7.4(01/05/08) / 0.9(02/07/03)
ruby-mode 1.25.2.8 / 1.58
Debian GNU/Linux Woody/Sarge

The relevant section of my .emacs looks as follows:

(require 'ruby-mode)
(require 'rubydb)

(setq auto-mode-alist
(cons '("\.rb$" . ruby-mode)
auto-mode-alist))

(setq interpreter-mode-alist (append '(("^#!.*ruby" . ruby-mode))
interpreter-mode-alist))
(autoload 'run-ruby “inf-ruby” “Run an intferior Ruby process”)
(autoload 'inf-ruby-keys “inf-ruby”
“Set local key defs for inf-ruby in ruby-mode”)
(add-hook ‘ruby-mode-hook
’(lambda()
(inf-ruby-keys)))

Any help highly appreciated!

···


I’ve seen things you people wouldn’t believe. Attack ships on fire off
the shores of the Nonestic Ocean. I watched magic blunderbusses flash
and glitter in the dark near Glindea’s Palace. All those moments will
be lost in time, like tears in rain. Time to die…

Hi,

···

In message “ruby-mode / inferior ruby” on 02/08/13, Bjørn Nordbø bn@strangedays.no writes:

Emacs’ ruby-mode doesn’t seem to work too well. I would be most happy if
anyone could tell me if what I see is the normal behaviour, or if there
is an error in my setup. An example, given the following code:

The author of inf-ruby.el has not been appeared on the list for a
while, so that I’m afraid no one is maintaining inf-ruby.el.

Keiju knows your request and is working on it. Stay tuned (but
without too much hope ;-).

						matz.

Thank you very much for your reply. A few short questions:

  • Is this a known issue with inf-ruby; ie. it’s not yet supported?
  • Is it inf-ruby’s job to keep track of the line numbers, ie. rewrite
    and present the output from irb?

If yes to both, I’ll try to take a look at it meself. I have never done
anything in Emacs Lisp, but I know a little Common Lisp and Scheme.

Again; thank you!

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

on 02/08/13, Bjørn Nordbø <bn@strangedays.no> writes:

Emacs’ ruby-mode doesn’t seem to work too well. I would be most happy if
anyone could tell me if what I see is the normal behaviour, or if there
is an error in my setup. An example, given the following code:

The author of inf-ruby.el has not been appeared on the list for a
while, so that I’m afraid no one is maintaining inf-ruby.el.

Keiju knows your request and is working on it. Stay tuned (but
without too much hope ;-).


I’ve seen things you people wouldn’t believe. Attack ships on fire off
the shores of the Nonestic Ocean. I watched magic blunderbusses flash
and glitter in the dark near Glindea’s Palace. All those moments will
be lost in time, like tears in rain. Time to die…

This isn’t an answer to your problem, but you may find it useful
nonetheless.

I generally don’t find loading a selected chunk of the buffer too helpful.
What I do instead is load the whole buffer into the inferior process.
Here’s the section of my .xemacs/init.el devoted to ruby-mode:

;;;; ruby-mode

(defun g0-ruby-load-buffer ()
"Save current buffer, and load it into an inferior ruby process.

If there is no inferior ruby process running, start one. Otherwise,
load the current buffer into the currently running process. Switch to
`ruby-buffer’."
(interactive)
(save-buffer)
(let ((bufname (buffer-name))
(pop-up-windows t))
(run-ruby ruby-program-name)
(ruby-load-file bufname)))

(defun g0-ruby-end-block ()
"Insert an indented “end” on its own line.

If the current line contains only whitespace, insert it on the current
line. Otherwise, insert the “end” line after the current line.

Insert a newline after the “end” and leave point after it."
(interactive)
(unless (string-match “^[ \t\r\f]*$”
(buffer-substring (point-at-bol) (point-at-eol)))
(end-of-line)
(insert ?\n))
(insert “end”)
(indent-for-tab-command))

(defun g0-ruby-toggle-irb-program ()
“Toggle `ruby-program-name’ between “ruby” and “ruby1.7”.”
(interactive)
(setq ruby-program-name (if (and (boundp 'ruby-program-name)
(string= ruby-program-name “irb
–inf-ruby-mode”))
“irb1.7 --inf-ruby-mode”
“irb --inf-ruby-mode”))
(message “Using ruby: %s” ruby-program-name))

;; for some ghastly reason, inf-ruby.el adds to ruby-mode-hook a
;; lambda that calls inf-ruby-keys, which resets the default
;; ruby-mode-map.
(setq ruby-mode-hook nil)
(add-to-hook 'ruby-mode-hook
(define-key ruby-mode-map [(control ?c) (control ?l)]
'g0-ruby-load-buffer))
(add-to-hook 'ruby-mode-hook
(define-key ruby-mode-map [(meta return)] 'g0-ruby-end-block))
(add-to-hook 'ruby-mode-hook
(define-key ruby-mode-map [(control ?c) (control ?p)]
'g0-ruby-toggle-irb-program))
(add-to-hook 'inferior-ruby-mode-hook
(define-key ruby-mode-map [(control ?c) (control ?l)]
'g0-ruby-load-buffer))
(add-to-hook 'inferior-ruby-mode-hook
(define-key ruby-mode-map [(meta return)] 'g0-ruby-end-block))
(add-to-hook 'inferior-ruby-mode-hook
(define-key ruby-mode-map [(control ?c) (control ?p)]
'g0-ruby-toggle-irb-program))

Couple of notes:

  • This defines keys:
    • C-cC-l – throw the current buffer into IRB.
    • C-cC-p – toggle the ruby-program-name between “ruby” and “ruby1.7”.
    • M-RET – Shove an “end” in the buffer, with a bit of intelligence
      with regard to placement. It won’t not insert it if
      there’s no matching def/class/begin/…, though. Might
      add that later.
  • I wrote this for XEmacs. It mightn’t work on FSF Emacs.
  • That nasty comment about inf-ruby.el may no longer apply. I wrote this
    a few versions ago. Perhaps I’m the evil one for thinking it’s evil,
    anyway.

Hi,

···

In message “Re: ruby-mode / inferior ruby” on 02/08/14, Bjørn Nordbø bn@strangedays.no writes:

Thank you very much for your reply. A few short questions:

  • Is this a known issue with inf-ruby; ie. it’s not yet supported?
  • Is it inf-ruby’s job to keep track of the line numbers, ie. rewrite
    and present the output from irb?

If yes to both, I’ll try to take a look at it meself. I have never done
anything in Emacs Lisp, but I know a little Common Lisp and Scheme.

The answers are “yes” and “don’t know yet”. The latter is perhaps
done by coordination of irb and inf-ruby.el.

						matz.

Thanks a lot! It worked after substituting add-to-hook with add-hook.
Still some minor glitches though, but definately useful!

···

George Ogata g_ogata@optushome.com.au wrote:

This isn’t an answer to your problem, but you may find it useful
nonetheless.


I’ve seen things you people wouldn’t believe. Attack ships on fire off
the shores of the Nonestic Ocean. I watched magic blunderbusses flash
and glitter in the dark near Glindea’s Palace. All those moments will
be lost in time, like tears in rain. Time to die…

Bjørn Nordbø wrote:

Thanks a lot! It worked after substituting add-to-hook with add-hook.
Still some minor glitches though, but definately useful!

Whoops, that was my own little macro. This should go before that code:

(defmacro add-to-hook (hook &rest forms)
"Add FORMS, as a lambda expression, to HOOK.

The lambda expression takes no arguments."
`(add-hook ,hook (lambda () ,@forms)))

Sounds like you worked it out, though.