# Ruby scratch buffer

Even though I do some programming in bash, JavaScript, elisp, etc, at the end of the day, my go-to language is Ruby. When I need to do a quick and dirty script, calculation or every time I need to understand something about the language or gems, I sometimes create a Ruby file and operate there, using Ruby, rcodetools and/or seeing_is_believing (more n those in a future post).

But Emacs has a very useful "tool" at our disposal: The Scratch Buffer.

The Scratch Buffer is (unsurprisingly) a buffer meant to be used and thrown away in every emacs session. It's meant to perform some calculation, test, transient configuration in elisp and be discarded.

Since I'm working in Ruby most of my time, I've configured it to use enh-ruby-mode by default.

# Basic setup

Here's a very basic setup. We first tell Emacs to use enh-ruby-mode for the scratch buffer and then we set an initial message.

  (setq initial-major-mode 'enh-ruby-mode)

(with-current-buffer "*scratch*"
(insert "# Welcome to the scratch buffer. Use it at will\n\n"))

Of course, this can be tweaked to use any other available mode and to print any message that we want (or no message at all).

# My setup

Here's the configuration I use:

  (setq initial-major-mode 'enh-ruby-mode)

(with-current-buffer "*scratch*"
(insert "RUBY_VERSION  # =>\n")
(if (fboundp 'xmp)
(xmp)))

It's pretty similar, but I use the xmp command to show me my system's Ruby version when Emacs creates the buffer. This keeps me aware of the version I have available out of the box. A small tweak, but I really dig it.

# Jumping to the scratch buffer

Finally, here's an extra tip, I have set up a keybinding C-x C-w to switch to the scratch buffer from wherever I am.

  (global-set-key (kbd "C-x C-w")
(lambda () (interactive) (switch-to-buffer "*scratch*")))

I hope you found this short post useful.

Saluti.