Skip to content

Instantly share code, notes, and snippets.

@jjrussell
Last active August 29, 2015 14:19
Show Gist options
  • Save jjrussell/28879ca4dbc812ae5086 to your computer and use it in GitHub Desktop.
Save jjrussell/28879ca4dbc812ae5086 to your computer and use it in GitHub Desktop.

This is how I ruby on Emacs. This is all based on top of the the built in ruby-mode. I’m using emacs 25 built by homebrew on OSX.

##ELPA Packages##

  • rspec-mode - Enhance ruby-mode for RSpec
  • ruby-block - highlight matching block
  • ruby-compilation - run a ruby process in a compilation buffer
  • ruby-end - Automatic insertion of end blocks for Ruby
  • ruby-interpolation - Ruby string interpolation helpers
  • ruby-tools - Collection of handy functions for ruby-mode
  • rvm - sets ruby used by emacs using rvm
  • projectile - Not ruby related but a fantastic “project” package that exposes a bunch of functions for navigating around a project without ever having to configure what a project is. It finds .git roots and assumes stuff. Very nice.

##Other ELPA packages##

  • rsense - More in depth ruby analysis using inferior ruby process. Has the potential to be much better than tags for navigation and completion but my experience with it wasn’t great. Slow and unreliable. That was over a year ago and I may have had it configured wrong.
  • robe - Similar to above. Probably better put together. Focused on rails. Haven’t tried it much. It actually starts a rails console for your project and uses that to search around.

ruby hooks Emacs lisp configuration for when ruby major mode is used.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Ruby
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq default-ruby-version "2.1.5")

(defun my-ruby-mode-hook ()
  (interactive)
  (setq require-final-newline nil)
  (require 'rspec-mode)
  (auto-fill-mode -1)

  ;; rsense suddenly got unstable 2013-09-07 would hang on autocomplete
  ;; (setq rsense-home (concat jjr-site-lisp-dir "/rsense-0.3"))
  ;; (add-to-list 'load-path (concat rsense-home "/etc"))
  ;; (require 'rsense)
  ;; (add-to-list 'ac-sources 'ac-source-rsense-method)
  ;; (add-to-list 'ac-sources 'ac-source-rsense-constant)
  (ruby-end-mode)
  (require 'ruby-interpolation) ; electric #{} in ruby strings by entering #

  ;; highlight beginging of blocks when point is on end
  ;; Requires ruby-end package
  (setq ruby-block-highlight-toggle 'overlay) ; other option is minibuffer or t for both

  (local-set-key "\r" 'newline-and-indent)
  (flymake-ruby-load)
  (add-to-list 'ac-sources 'ac-source-etags)
  )

;; Set emacs environment to use the default rvm ruby
(rvm-use-default)

(add-hook 'ruby-mode-hook 'my-ruby-mode-hook)

(defun update-ruby-tags ()
  "Find the project root using projectile and if it looks like a ruby project then update the tags
in an asynchronous process.

Dependent emacs packages:
- projectile for finding project root

Dependent gems:
- ripper-tags a much better ruby tagger than ctags"
  (interactive)
  (let ((project-root (funcall 'projectile-project-root)))
    (if (and project-root
             (or (file-exists-p (expand-file-name ".ruby-version" project-root ))
                 (file-exists-p (expand-file-name "Gemfile" project-root ))
                 ))

        (let* ((default-directory project-root)
	       ;;(ripper-tags-executable (concat (getenv "HOME") "/.rvm/bin/rvm " default-ruby-version " do ripper-tags"))
	       (ripper-tags-executable "rvm default do ripper-tags")
               (ruby-tags-command (concat "BUNDLE_GEMFILE='' " ripper-tags-executable " -R --exclude=db/migrate --tag-file=TAGS"))
               )
          (message (concat "Updating tags file in " project-root))
	  (call-process-shell-command (concat ruby-tags-command "&") nil 0)
          ;;same command but use this instead to debug any problems as output goes to a buffer
                                        ;(async-shell-command ruby-tags-command nil)
          )
      )
    )
  )


(add-hook 'ruby-mode-hook
          (lambda ()
            (add-hook 'after-save-hook 'update-ruby-tags nil t)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment