10
votes

How can I run a ruby piece of code from a buffer, without actually saving the buffer in a file? A scenario would be to a) switch to the scratch buffer b) M-x ruby-mode c) type ruby code d) "compile" the buffer and print results in another buffer. I don't want to save the buffer contents in a file and then "compile" that file

UPDATE 1-9-2011 Which are the latest versions of ruby-mode and inf-ruby and where can I get them? I use the ubuntu natty version of ruby mode and the elpa version of inf-ruby of emacs 23.2. On a clean emacs config, the following config (see underneath) fail with:

can't convert nil into String 
from (irb):1:in `eval' 
from (irb):1". 

Configuration is just the following, no other configuration directives in emacs.d:

(require 'ruby-mode)
(load-file "Configs/.emacs.d/elpa/inf-ruby-2.1/inf-ruby.el")

(autoload 'inf-ruby "inf-ruby" "Run an inferior Ruby pathrocess" t)
(autoload 'inf-ruby-keys "inf-ruby" "" t)
(eval-after-load 'ruby-mode
  '(add-hook 'ruby-mode-hook 'inf-ruby-keys))
2
for me, inf-ruby doesn't seems to work properly for a buffer without a corresponding file e.g. if I M-x ruby-mode the scratch buffer and then M-x inf-ruby it doesn't work. Howecer, it works for ruby files.Vassilis
how do you found inf-ruby compare to ruby-compilation? Seems we are looking same thing but more people recommend inf-ruby?Paul L
there is a beautiful piece of emacs lisp at github.com/jimweirich/emacs-setup-esk/blob/master/… . You can evalulate a ruby/clojure/coffeescript/javascript buffer and it was dead easy to integrate it to my emacs setup. It evaluates the file the buffer displays though. Thanks Jim!Vassilis

2 Answers

18
votes

You can do this with inf-ruby.el.

With inf-ruby installed, M-x inf-ruby starts a new ruby interpreter in the background, to which you can send code using the ruby-send-* commands, such as ruby-send-region (bound by default to C-c C-r). To send the whole buffer, select all with C-x h then hit C-c C-r.

Update, 2011-09-02: The latest version of inf-ruby.el (2.1.1, available in ELPA) is that maintained by 'nonsequitur' on github, and snapshot packages are available in Melpa.

3
votes

A few options for you to try

1. Rinari from Github

It claims to be

aimed towards making Emacs into a top-notch Ruby and Rails development environment


2. ruby-compilation.el is what you need.

There's a thread on that here which I have used and found helpful.

Chris