Random Ruby tips from trenches #1
by Hemant,
-
Rubocop and Flycheck :
Flycheck is a emacs mode which helps
us with IDE like warnings in Emacs. I am already using enh-ruby-mode which helps
with some of the syntax errors and stuff, but what is nice about flycheck is
it integrates with rubocop and shows rubocop errors in-place in the editor.
A picture is worth thousand words so:

-
pry ––gem:
pry --gem opens a pry session with ./lib added to $LOAD_PATH and 'require's the gem.
A good shortcut while working on gems and you want a quick console with the gem loaded.
-
ruby –S :
This can be used for running binaries which are present in local directory. For example,
if you are working on bundler and want to run bundle command with local version of
bundler rather than one installed globally you can use:
ruby -S ./bin/bundle -I ./lib/ install
The advantages are:
-
ruby -Sallows you to ignore#!/usr/bin/env rubyline and even if current version of ruby is X usingruby -Syou can run the command with another version of Ruby. Especially useful for running scripts with JRuby, Rbx etc. -
Based on
RUBYPATHenvironment variable running a script viaruby -S(orjruby -S) allowsPATHenvironment to be modified. For example runningjruby -S gemdoes not rungemwhich is in current path, but it runsgemcommand provided by JRuby because JRuby defines differentRUBYPATH.
-
Faster rbenv load :
If you are using rbenv and there is a lag while opening a new shell, consider
updating the rbenv initializing line in your shell profile to:
eval "$(rbenv init - --no-rehash)"
The --no-rehash flag tells rbenv to skip automatic rehash when opening a new
shell and speeds up the process. This also speeds up VIM if you are using
vim-rails or
vim-ruby.