Running Ruby script in emacs

D

duke

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...
 
R

Ryan Davis

Hey ...
=20
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.
=20
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.
 
D

Duke

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 ...
 
D

duke

Have you looked into ruby-compilation.el?  The version on emacswiki.orgis
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!
 
D

duke

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'tseem
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". :) Thanks for your input.
 
R

Ryan Davis

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.

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)
 
D

duke

It executes OK, and the program output is correct, but I may be taking
"the long way around". :) 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 (fromhttps://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 twill
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.  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!
 
D

duke

It executes OK, and the program output is correct, but I may be taking
"the long way around". :) 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 (fromhttps://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 twill
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.

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!
 
D

duke

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 <return> 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.
 

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top