Ruby interpolation

In Ruby, you can in double quoted strings interpolate. For example:

"1 + 2 = #{1 + 2}" # "1 + 2 = 3"
'1 + 2 = #{1 + 2}' # "1 + 2 = \#{1 + 2}"

I made a function ruby-interpolate that inserts #{} and places the cursor between { and } if the cursor is in a double quoted string.

(defun ruby-interpolate ()
  "In a double quoted string, interpolate."
  (interactive)
  (insert "#")
  (when (and
         (looking-back "\".*")
         (looking-at ".*\""))
    (insert "{}")
    (backward-char 1)))

Bind the key:

(define-key ruby-mode-map (kbd "#") 'ruby-interpolate)

blog comments powered by Disqus Back to Top

Fork me on GitHub