Running Ruby script in emacs

Hey …

I’m taking my first steps with Ruby. I have ruby-mode loaded and
working. I also have run-ruby, but we’ll forget about that for the
moment.

I have a working script that I want to “compile” and run while in ruby-
mode. I grok how to compile it - no worries there. The script asks for
input from the user. That’s what I don’t know how to do! Is there a
way to input a value while in the ‘compilation’ process? TIA…

On Apr 14, 2011, at 10:41 , duke wrote:

Hey …

I’m taking my first steps with Ruby. I have ruby-mode loaded and
working. I also have run-ruby, but we’ll forget about that for the
moment.

I have a working script that I want to “compile” and run while in ruby-
mode. I grok how to compile it - no worries there. The script asks for
input from the user. That’s what I don’t know how to do! Is there a
way to input a value while in the ‘compilation’ process? TIA…

M-x compile doesn’t allow for interactive use, so you don’t want that.

I use M-x shell for almost all these scenarios (and nearly all other
shell use, really). You can also look at my autotest.el if you’re
wanting to make something shell-like but more tweaked out for your use.
You can find that on emacswiki.

On Apr 14, 1:04pm, Ryan D. [email protected] wrote:

input from the user. That’s what I don’t know how to do! Is there a
way to input a value while in the ‘compilation’ process? TIA…

M-x compile doesn’t allow for interactive use, so you don’t want that.

OK!

I use M-x shell for almost all these scenarios (and nearly all other
shell use, really). You can also look at my autotest.el if you’re
wanting to make something shell-like but more tweaked out for your
use. You can find that on emacswiki.

Much obliged! Thanks …

On 14-Apr-2011, duke [email protected] wrote:

Hey …

I’m taking my first steps with Ruby. I have ruby-mode loaded and
working. I also have run-ruby, but we’ll forget about that for the
moment.

I have a working script that I want to “compile” and run while in ruby-
mode. I grok how to compile it - no worries there. The script asks for
input from the user. That’s what I don’t know how to do! Is there a
way to input a value while in the ‘compilation’ process? TIA…

Have you looked into ruby-compilation.el? The version on emacswiki.org
is
old, but you can either install it via ELPA, or from
rinari/util at master · eschulte/rinari · GitHub directly. You can
then
M-x ruby-compilation-this-buffer and it will execute your script in a
compilation buffer. You can input data in the compilation window, but
note
that you might find that RET doesn’t do the job because it’s been
shadowed
by a different mode. If this happens to you, you will need to rebind
comint-send-input.

On 15-Apr-2011, duke [email protected] wrote:

Thank you so much! I’ll give it a try, for sure!

There are a number of other good packages for Ruby in ELPA as well. I
think
you might have to manually install mode-compile.el, IIRC. ELPA doesn’t
seem
to supply that, but I think this package depends on it. See
http://www.emacswiki.org/emacs/ModeCompile

On Apr 14, 4:42pm, “mouser” [email protected] wrote:

input from the user. That’s what I don’t know how to do! Is there a
way to input a value while in the ‘compilation’ process? TIA…

Have you looked into ruby-compilation.el? The version on emacswiki.org is
old, but you can either install it via ELPA, or
fromhttps://github.com/eschulte/rinari/tree/master/utildirectly. You can then
M-x ruby-compilation-this-buffer and it will execute your script in a
compilation buffer. You can input data in the compilation window, but note
that you might find that RET doesn’t do the job because it’s been shadowed
by a different mode. If this happens to you, you will need to rebind
comint-send-input.

Thank you so much! I’ll give it a try, for sure!

On Apr 15, 12:55pm, “mouser” [email protected] wrote:

On 15-Apr-2011, duke [email protected] wrote:

Thank you so much! I’ll give it a try, for sure!

There are a number of other good packages for Ruby in ELPA as well. I think
you might have to manually install mode-compile.el, IIRC. ELPA doesn’t seem
to supply that, but I think this package depends on it.
Seehttp://www.emacswiki.org/emacs/ModeCompile

I installed ruby-compilation.el from ELPA, after installing support
for the latter. I fired some Ruby code, and it works! but …

Your were right …

If this happens to you, you will need to rebind comint-send-input.

What do you mean by “rebind comint-send-input”?

The way I’m doing it is:

after I’m done inputting data into the program, I do a M-x comint-send-
input, instead of a `cr’. Is that correct?

It executes OK, and the program output is correct, but I may be taking
“the long way around”. :slight_smile: Thanks for your input.

On Apr 16, 2011, at 13:45 , mouser wrote:

immediately apparent where that is happening.
It’d be a lot more maintainable if you didn’t modify the file and
instead added the define-key to your .emacs:

(define-key ruby-compilation-minor-mode-map [return] 'comint-send-input)

On 16-Apr-2011, Ryan D. [email protected] wrote:

It’d be a lot more maintainable if you didn’t modify the file and instead
added the define-key to your .emacs:

(define-key ruby-compilation-minor-mode-map [return] 'comint-send-input)

Yes, that’s definitely better than my crude hack.

On 16-Apr-2011, duke [email protected] wrote:

It executes OK, and the program output is correct, but I may be taking
“the long way around”. :slight_smile: Thanks for your input.

You might find the following will make your life easier.

Uninstall ruby-compilation from ELPA (hit ‘d’ on it’s line in
package-list-packages, then hit ‘x’). Then, install the source for
ruby-compilation (from
https://github.com/eschulte/rinari/raw/master/util/ruby-compilation.el)
in
your load path (somewhere in ~/.emacs.d for instance). Next, add
“(require
'ruby-compilation)” to ~/.emacs. Finally, open up the
ruby-compilation.el
file in your load path and add “(define-key map [return]
'comint-send-input)” to defvar ruby-compilation-minor-mode-map (for
instance
at line 279). The last part is required because for some reason
compilation-mode shadows the comint-send-input binding though it’s not
immediately apparent where that is happening.

This gives you a couple advantages over installing it from ELPA. C-x t
will
invoke ruby-compilation-this-buffer whenever you are in ruby-mode, and
now
hitting return/enter will invoke comint-send-input in the compilation
buffer.

By the way, you might find the following useful for learning and
programming
Ruby in Emacs. See EmacsWiki: Flymake Ruby for
automated
syntax checking. Also, install the ruby-debug (or ruby-debug19 for Ruby
1.9) gem for Ruby debugging. Take a look at
Debugging with rdebug from GNU Emacs for Emacs
integration of this debugger.

On Apr 16, 2:43pm, “mouser” [email protected] wrote:

your load path (somewhere in ~/.emacs.d for instance). Next, add "(require
buffer.

By the way, you might find the following useful for learning and programming
Ruby in Emacs. Seehttp://www.emacswiki.org/emacs/FlymakeRubyfor automated
syntax checking. Also, install the ruby-debug (or ruby-debug19 for Ruby
1.9) gem for Ruby debugging. Take a look
athttp://bashdb.sourceforge.net/ruby-debug/rdebug-emacs.htmlfor Emacs
integration of this debugger.

Much obliged! I’ll give it a shot tomorrow!

On 18-Apr-2011, duke [email protected] wrote:

OK! Followed your instructions to the letter …
and hacked my .emacs for the key map stuff, as per Ryans advice …
NO joy!

It sounds like return is still bound to compile-goto-error. Did you
restart
Emacs after modifying your .emacs file? You might want to check your
keybindings in the compilation buffer. Get it in focus and hit C-h b,
and a
new window will be created with your current keybindings. Look under
ruby-compilation-minor-mode in the keybinding buffer and see if there is
an
entry for key and binding comint-send-input.

On Apr 18, 3:15pm, “mouser” [email protected] wrote:

ruby-compilation-minor-mode in the keybinding buffer and see if there is an
entry for key and binding comint-send-input.

Everything is now working great! I had simply re-loaded my .emacs file
thinking that doing so would be enough. Once I re-started “emacs”,
well things couldn’t be much better! Thanks a bunch.

On Apr 16, 2:43pm, “mouser” [email protected] wrote:

your load path (somewhere in ~/.emacs.d for instance). Next, add "(require
buffer.
OK! Followed your instructions to the letter …
and hacked my .emacs for the key map stuff, as per Ryans advice …
NO joy!

In Messages buffer, I get:

“compilation-next-error: No error here”

after being asked for the required input.

The script does work BTW. Any ideas?

By the way, you might find the following useful for learning and programming
Ruby in Emacs. Seehttp://www.emacswiki.org/emacs/FlymakeRubyfor automated
syntax checking. Also, install the ruby-debug (or ruby-debug19 for Ruby
1.9) gem for Ruby debugging. Take a look at
http://bashdb.sourceforge.net/ruby-debug/rdebug-emacs.htmlfor Emacs
integration of this debugger.

Thanks a lot for all those links. They surely will be useful to me.
Much obliged!