Difference between revisions of "Ruby"
(Rinari - Rinari Is Not A Rails IDE) |
(→See also: code folding) |
||
(13 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | We'll show here how to configure Emacs as a productive environment for Ruby and Rails development. | ||
+ | |||
+ | Be also aware that [[Starter Kits]] like [[Prelude]] have many stuff pre-installed, and that [https://spacemacs.org/layers/+frameworks/ruby-on-rails/README.html Spacemacs has a Ruby layer]. | ||
+ | |||
== ruby-mode == | == ruby-mode == | ||
− | '''{{ModeLink|ruby-mode}}''' provides basic font-locking, indentation and navigation support for Ruby source code. It was first included in Emacs 23 (previously it was a third party package, maintained by Ruby's developers). While {{Mode|ruby-mode}} is fairly barebone package it could be augmented by a multitude of minor modes | + | '''{{ModeLink|ruby-mode}}''' provides basic font-locking, indentation and navigation support for Ruby source code. It was first included in Emacs 23 (previously it was a third party package, maintained by Ruby's developers). While {{Mode|ruby-mode}} is fairly barebone package it could be augmented by a multitude of minor modes (see below). |
+ | |||
+ | == ruby-end - auto insert end statements == | ||
+ | |||
+ | Just install [http://github.com/rejeep/ruby-end ruby-end] (in [[ELPA]]). Now ruby-end-mode will start alongside ruby-mode. | ||
+ | |||
+ | == Ruby on Rails == | ||
+ | |||
+ | === [[Rinari]] === | ||
+ | |||
+ | [https://github.com/eschulte/rinari Rinari] (in melpa) stands for Rinari Is Not A Rails IDE. It is an Emacs minor mode for Rails that is actively maintained. | ||
+ | |||
+ | Rinari features: | ||
+ | |||
+ | * for 'jumps' between Model, View, Controller and their RSpec examples and Test Unit tests, | ||
+ | * as well as Cucumber steps and features. | ||
+ | * running of a console session with full Rails application loaded for interactive use, | ||
+ | * SQL console, | ||
+ | * starting of the web server (rails server). | ||
+ | |||
+ | For a list of keybindings, see [[Rinari]]. The [http://rinari.rubyforge.org/ official documentation] is alright, but for best information check out the source. | ||
+ | |||
+ | === Projectile-rails - navigation, rake tasks, console, etc === | ||
+ | |||
+ | [[Projectile]] is a popular project management system. It allows to easily search files under a project, switch buffers pertaining to that project, switch to tests files, search in project (with <code>rgrep</code>, <code>ag</code>,…), and more. Now we can add Rails support with [https://github.com/asok/projectile-rails projectile-rails] (in melpa). | ||
+ | |||
+ | With Projectile Rails you are able to: | ||
+ | |||
+ | * navigate through rails resources (controllers, views, helpers and so on) | ||
+ | * jump to ruby classes and template files | ||
+ | * run rake | ||
+ | * run rails console | ||
+ | * run rails dbconsole | ||
+ | * run rails generate | ||
+ | * run rails server | ||
+ | * open log files with auto-revert-mode on | ||
+ | * see rails keywords highlighted | ||
+ | * take advantage of zeus and spring preloaders | ||
+ | |||
+ | It can be a replacement for rinari. | ||
+ | |||
+ | == Intelligent code navigation and completion with Robe == | ||
+ | |||
+ | [https://github.com/dgutov/robe Robe] (in MELPA) is a code assistance tool that uses a Ruby REPL subprocess with your application or gem code loaded, to provide information about loaded classes and modules, and where each method is defined. | ||
+ | |||
+ | To load it with ruby-mode: | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (require 'robe) | ||
+ | (add-hook 'ruby-mode-hook 'robe-mode) | ||
+ | </source> | ||
+ | |||
+ | If you are using rvm you may want to instruct robe to auto-trigger <code>rvm-activate-corresponding-ruby</code>: | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (defadvice inf-ruby-console-auto (before activate-rvm-for-robe activate) | ||
+ | (rvm-activate-corresponding-ruby)) | ||
+ | </source> | ||
+ | |||
+ | === Code navigation === | ||
+ | |||
+ | By default bound to '''M-.''' (robe-jump). It can be used to jump to definitions of various classes and methods. | ||
+ | |||
+ | === Code completion === | ||
+ | |||
+ | For intelligent completion robe can be integrated with company mode. Company mode is a modern and modular completion system for emacs which accepts a multitude of pluggable back-ends and front-ends to provide rich completions on the fly. Robe provides a backend to the completion in form of company-robe. This can be configured as follows : | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (global-company-mode t) | ||
+ | (push 'company-robe company-backends) | ||
+ | </source> | ||
+ | |||
+ | Now all you have to do is hit robe-start when you navigate into your project. After that at any point if you trigger company-robe you should be greeted with an intellisense like method definitions. | ||
+ | |||
+ | Completion will start automatically after you type a few letters. Use M-n, M-p, <tab> and <return> to complete. Search through the completions with C-s, C-r and C-o. Even the documentations are available right from the editor (Press F1 at any menu entry). | ||
+ | |||
+ | |||
+ | [[File:Ruby-robe.png]] | ||
+ | |||
+ | == Syntax checking: flymake == | ||
+ | |||
+ | See [[Flymake]], and install [https://github.com/purcell/flymake-ruby flymake-ruby] (in melpa). | ||
+ | |||
+ | To make sure it is started along with ruby mode, add: | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (require 'flymake-ruby) | ||
+ | (add-hook 'ruby-mode-hook 'flymake-ruby-load) | ||
+ | </source> | ||
+ | |||
+ | [[File:Ruby-flymake.png]] | ||
+ | |||
+ | == Ruby shell inside Emacs == | ||
+ | |||
+ | [https://github.com/nonsequitur/inf-ruby inf-ruby] provides a REPL buffer connected to a Ruby subprocess. It is available through the package manager. To bind to a shortcut, do something like: | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (global-set-key (kbd "C-c r r") 'inf-ruby) | ||
+ | </source> | ||
+ | |||
+ | See also: [[Shell]] | ||
+ | |||
+ | == RVM integration == | ||
+ | |||
+ | Pick [https://github.com/senny/rvm.el rvm.el]. Once installed you will just have to call rvm-activate-corresponding-ruby and rvm.el will automatically pick up your ruby version and gemset from your <code>.rvmrc</code> file. | ||
+ | |||
+ | For a keybinding: | ||
+ | |||
+ | <source lang="lisp"> | ||
+ | (global-set-key (kbd "C-c r a") 'rvm-activate-corresponding-ruby) | ||
+ | </source> | ||
− | + | [[File:Emacs-rvm.png]] | |
− | + | [[Category:Programming]] | |
+ | [[Category:Programming_languages]] | ||
− | + | == See also == | |
− | + | You might want to check the following pages to make the most out of Emacs: | |
+ | * [[Git]], [[Github]] and [[Gitlab]] | ||
+ | * [[Buffer_management]], code [[Folding]], | ||
+ | * how Emacs helps in [[Discoverability]], | ||
+ | * [[Compilation]] and [[Shell]] | ||
== External links == | == External links == |
Latest revision as of 10:34, 17 May 2017
We'll show here how to configure Emacs as a productive environment for Ruby and Rails development.
Be also aware that Starter Kits like Prelude have many stuff pre-installed, and that Spacemacs has a Ruby layer.
ruby-mode
ruby-mode provides basic font-locking, indentation and navigation support for Ruby source code. It was first included in Emacs 23 (previously it was a third party package, maintained by Ruby's developers). While ruby-mode is fairly barebone package it could be augmented by a multitude of minor modes (see below).
ruby-end - auto insert end statements
Just install ruby-end (in ELPA). Now ruby-end-mode will start alongside ruby-mode.
Ruby on Rails
Rinari
Rinari (in melpa) stands for Rinari Is Not A Rails IDE. It is an Emacs minor mode for Rails that is actively maintained.
Rinari features:
- for 'jumps' between Model, View, Controller and their RSpec examples and Test Unit tests,
- as well as Cucumber steps and features.
- running of a console session with full Rails application loaded for interactive use,
- SQL console,
- starting of the web server (rails server).
For a list of keybindings, see Rinari. The official documentation is alright, but for best information check out the source.
Projectile is a popular project management system. It allows to easily search files under a project, switch buffers pertaining to that project, switch to tests files, search in project (with rgrep
, ag
,…), and more. Now we can add Rails support with projectile-rails (in melpa).
With Projectile Rails you are able to:
- navigate through rails resources (controllers, views, helpers and so on)
- jump to ruby classes and template files
- run rake
- run rails console
- run rails dbconsole
- run rails generate
- run rails server
- open log files with auto-revert-mode on
- see rails keywords highlighted
- take advantage of zeus and spring preloaders
It can be a replacement for rinari.
Robe (in MELPA) is a code assistance tool that uses a Ruby REPL subprocess with your application or gem code loaded, to provide information about loaded classes and modules, and where each method is defined.
To load it with ruby-mode:
(require 'robe)
(add-hook 'ruby-mode-hook 'robe-mode)
If you are using rvm you may want to instruct robe to auto-trigger rvm-activate-corresponding-ruby
:
(defadvice inf-ruby-console-auto (before activate-rvm-for-robe activate)
(rvm-activate-corresponding-ruby))
By default bound to M-. (robe-jump). It can be used to jump to definitions of various classes and methods.
Code completion
For intelligent completion robe can be integrated with company mode. Company mode is a modern and modular completion system for emacs which accepts a multitude of pluggable back-ends and front-ends to provide rich completions on the fly. Robe provides a backend to the completion in form of company-robe. This can be configured as follows :
(global-company-mode t)
(push 'company-robe company-backends)
Now all you have to do is hit robe-start when you navigate into your project. After that at any point if you trigger company-robe you should be greeted with an intellisense like method definitions.
Completion will start automatically after you type a few letters. Use M-n, M-p, <tab> and <return> to complete. Search through the completions with C-s, C-r and C-o. Even the documentations are available right from the editor (Press F1 at any menu entry).
Syntax checking: flymake
See Flymake, and install flymake-ruby (in melpa).
To make sure it is started along with ruby mode, add:
(require 'flymake-ruby)
(add-hook 'ruby-mode-hook 'flymake-ruby-load)
Ruby shell inside Emacs
inf-ruby provides a REPL buffer connected to a Ruby subprocess. It is available through the package manager. To bind to a shortcut, do something like:
(global-set-key (kbd "C-c r r") 'inf-ruby)
See also: Shell
RVM integration
Pick rvm.el. Once installed you will just have to call rvm-activate-corresponding-ruby and rvm.el will automatically pick up your ruby version and gemset from your .rvmrc
file.
For a keybinding:
(global-set-key (kbd "C-c r a") 'rvm-activate-corresponding-ruby)
See also
You might want to check the following pages to make the most out of Emacs:
- Git, Github and Gitlab
- Buffer_management, code Folding,
- how Emacs helps in Discoverability,
- Compilation and Shell