My favourite Ruby books

Pickaxe Guide

Programming Ruby 1.9 & 2.0: The Pragmatic Programmers’ Guide

by Dave Thomas, Andy Hunt & Chad Fowler

The ‘pickaxe’ book is a complete overview to Ruby. It’s a pretty hefty book, covering these topics:
- A great tutorial on using Ruby.
- The definitive reference to the language.
- Complete documentation of all built-in classes, modules, and methods.
- Complete descriptions of all 97 standard libraries.


Practical Object Oriented Design in Ruby: An Agile Primer

by Sandi Metz

Ruby likes objects and ‘POODR’ shows how to make the most of them.
Modular code is easier to maintain, reuse and understand.
Sandi Metz has some interesting ideas on OOP - her rules have become important.
We see the world in objects, and I think that code usually makes more sense in objects too.


Ruby on Rails Tutorial: Learn Web Development with Rails

by Michael Hartl

This is a great introduction to Rails. It starts slow enough for complete n0obs, but has enough depth
to interest far more advanced programmers. It walks through TDD, getting to use the console and git,
and covers encryption, AJAX and how Rails works on a practical level.
There is also a free, online version of the book here

Eloquent Ruby

Eloquent Ruby

by Russ Olsen

One of the best things about Ruby is its expressiveness and elegance.
Understanding how Ruby was designed to work helps you to really tap into its
power and makes a lot of things make a lot more sense.

Vim Folds

I was originally scared of vim folds. They were confusing and hard to manage. And there were extra shortcuts to learn.

I’ve recently started playing with them and have found that they can be useful, especially with long documents.

The main shortcuts you need to learn are:

za - toggle current fold open/closed
zR - open all folds
zM - close all folds

This would work as an explicit fold:

# My super clever method {{{
def clever_method
  something clever
# }}}

The }}} triple curly braces then let the area fold into something like:

+--  7 lines: My super clever method -----------------

which lets you expand / contract it with za.

It’s probably worth adding za as a shortcut.

There’s loads more you can do with them, but this is a fairly easy way to start to understand them..

Git Diff for files

You can find the differences between files by using git diff:

git diff commit1 commit2

This will list all the differences, which could be a long list of changes.

If you’re looking for just the file names, you can use the –name-only flag:

git diff --name-only commit1 commit2

You can see which files have changed in the last couple of commits with:

git diff --name-only HEAD~2

Vim: View Last Commands

You can see your last commands by:
1. Pressing : (which is hopefully mapped to just ;)
2. <ctrl-p> (‘previous’ in many unix applications)
You can also press up if you find this easier… although you shouldn’t…

Another way is to press q:, which provides a list of your recent commands. You can k your way through them and open (<cr>) them.

Vim Text Objects

One of the most powerful features in Vim is text objects. These increase the number of ‘vim nouns’ that you can use with ‘vim verbs’.

Imagine your cursor is in the middle of a paragraph you want to delete. Instead of laboriously deleting single lines, you could just type dip for ‘ delete inner paragraph’. The text object represents the entire paragraph.

A list of these is:

iw inner word
it inner xml / html tag
is inner sentence
i' inner single quotation marks
i" inner double quotation marks
i{ inner { ... } braces
i[ inner [ ... ] braces
i( inner ( ... ) braces
ip inner paragraph
im inner ruby method (from the vim-textobj-rubyblock plugin)

The same commands also work with a for a (eg. ca( will also change a bracket, rather than just the contents of them)

For an in-depth explanation, type :h text-objects into vim.

Save time with Bash & !!

!! in Bash / Zsh works the same way as _ does in irb / Pry.

It substitutes the return value of the previous line into the current line.

Ruby - Hash#fetch

If you can’t be bothered to set a default on a hash, you can always use Hash#fetch to specify a default as you go.

These are the examples from the docs

h = { "a" => 100, "b" => 200 }
h.fetch("a")                            #=> 100
h.fetch("z", "go fish")                 #=> "go fish"
h.fetch("z") { |el| "go fish, #{el}"}   #=> "go fish, z"

I usually think it’s safer to use this rather than Hash#[] (which is just usuing the normal hash[:possibly_doesnt_exist] syntax), as it removes the chances of a rogue nil popping up.

Vim: Ctrl-R in Insert Mode

ctrl-r in Insert Mode is pretty useful.

It inserts text based on your current vim context.

Many of these commands work with registers, so it may be worth getting your head round those….

Ctrl-R Shortcut What it does
ctrl-r % inserts filename
ctrl-r : inserts last command
ctrl-r / inserts last search
ctrl-r = inserts evaluated sum (eg. 2+3)
ctrl-r [a-z 0-9 key] inserts text from register

Hope they help!

Vim Movement

There are a load of ways to move around in vim.
The first things you learn (h, j, k & l) generally aren’t that efficient. word, end, till, / (buffer search) & f (line search) are usually better.

On top of these, there’s a handful of other, lesser-known commands:

z commands (also used for folding)

zz move current line to middle of screen
zt move current line to top of screen
zb move current line to bottom of screen

Moving over lines quickly

ctrl-e move up 1 line
ctrl-y move down 1 line
ctrl-u move up 1/2 page
ctrl-d move down 1/2 page
ctrl-b move up 1 page
ctrl-f move down 1 page

Hide files from Git Index

If you want to hide an already-commited file in Git, you can:

git update-index --assume-unchanged <file>

This lets you alter the files and you won’t be at (too much) risk of committing those changes by mistake.

This can be useful if you want a fairly quick way of hiding environmental variables… But be careful…