Setting up a Ruby Dev Environment

Chances are you can skip this chapter If you do then you will need the following installed and configured to your liking; ruby 2.x and git.

On OS X

Installing Homebrew

If you haven't already, install Homebrew.

In terminal run the following:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing Git

Run:

$ brew install git

Then make sure to set the configuration to your details:

$ git config --global color.ui true
$ git config --global user.name "YOUR NAME"
$ git config --global user.email "YOUR@EMAIL.com"

Now comes the hard part where you are likely to screw something up; configuring SSH with GitHub:

GitHub has an excellent guide for SSH stuff.

First generate a new key:

$ ssh-keygen -t rsa -C "YOUR@EMAIL.com"

It is a good idea to check for existing keys first:

$ ls -al ~/.ssh

If you see keys already there then figure out what those keys are before you proceed

After some prompts you will have a new key.

Add the key to ssh-agent:

# start the ssh-agent
$ eval "$(ssh-agent -s)"

# add the key
$ ssh-add ~/.ssh/id_rsa

Then you will need to add it to your GitHub account. You can quickly copy it to the clipboard by using pbpaste:

$ pbcopy < ~/.ssh/id_rsa.pub

Check that things are working by running:

$ ssh -T git@github.com

Installing Ruby

To install and manage ruby we will be using rbenv

Install it using brew:

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

Make sure rbenv init is called with every bash session:

$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

Zsh note: Modify ~/.zshrc instead of ~/.bash_profile.

Now install ruby 2.4.0 and set it as the global version:

$ rbenv install 2.4.0
$ rbenv global 2.4.0

On Ubuntu

First install the dependencies:

$ sudo apt-get update
$ sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libcurl4-openssl-dev

Installing Git

Run:

$ sudo apt-get install git

Then make sure to set the configuration to your details:

$ git config --global color.ui true
$ git config --global user.name "YOUR NAME"
$ git config --global user.email "YOUR@EMAIL.com"

Now comes the hard part where you are likely to screw something up; configuring SSH with GitHub:

GitHub has an excellent guide for SSH stuff.

First generate a new key:

$ ssh-keygen -t rsa -C "YOUR@EMAIL.com"

It is a good idea to check for existing keys first:

$ ls -al ~/.ssh

If you see keys already there then figure out what those keys are before you proceed

After some prompts you will have a new key.

Add it to ssh-agent:

# start the ssh-agent
$ eval "$(ssh-agent -s)"

# add the key
$ ssh-add ~/.ssh/id_rsa

Then will need to add it to your GitHub. You can quickly copy it by using xclip:

$ sudo apt-get install xclip
# Downloads and installs xclip

$ xclip -sel clip < ~/.ssh/id_rsa.pub

Check that things are working by running:

$ ssh -T git@github.com

Installing Ruby

Install rbenv by cloning it:

$ cd
$ git clone git://github.com/sstephenson/rbenv.git .rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL

$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
$ exec $SHELL

Zsh note: Modify ~/.zshrc instead of ~/.bashrc.

When you install rbenv this way you can upgrade it by git pulling. For example:

$ cd ~/.rbenv
$ git pull

The same goes for ruby-build:

$ cd ~/.rbenv/plugins/ruby-build
$ git pull

The master branch is always working (yeah rational branching!) so you don't have to worry about things exploding.

Set the ruby version to 2.4.0:

$ rbenv install 2.4.0
$ rbenv global 2.4.0

Testing

We will be testing all our code using rspec. A lot of the tests we write will serve as a form of documentation; we will know what we are going to build before we will build it. We will follow http://betterspecs.org and do our best to conform to best practices.

First install rspec globally:

$ gem install rspec

Be sure to run $ rbenv rehash after

When there is a spec and it's not clear where to put it, drop it in a folder named spec. You can then run rspec against the spec folder directly:

$ rspec

Deployment

Deployment tools are often complicated and messy. They abstract our apps behind countless layers of abstraction and leave us with no real idea how they work. Most of this abstraction is there to solve problems 99% of us will never have. Most of the problems end up having are caused by abstractions meant to solve problems 99% of us will never have.

We will keep our tools simple and straightforward. For configuring/booting our apps we will use Rackup. Managing our app's processes will be done with a Procfile. To deploy our apps we will use heroku buildpacks. App settings will be stored in an ENV. Locally we will manage processes and our env using foreman.

Heroku build packs can be used on any host with Docker and Buildstep

Configuration

Most frameworks handle configuration with yaml or ruby files that are placed in environments/ or config/ and then loaded and parsed. This is overkill. If you cant accomplish your config using env vars then you are structuring your apps wrong.

For example, database configuration is typically done like this:

production:
  mongoid:
    database: "database"
    port: ""
    url: ""

When you adopt the constraint of env vars you realize that this multiline yaml configuration could be done with a single uri string. Instead of initializing your database with three or four lines you just pass in one:

client = MongoClient.from_uri(ENV['MONGO_URI'])

Most Heroku addons provide their configuration through env vars.

Tools like foreman make the management of ENV vars dead simple. There is no excuse to adopt an overly complex configuration system. /rant