Optimize your Rails on OS X

Getting your development environment setup just the way you like it takes some time.   I’ve done it several times recently and this is the setup I feel most productive with.

The Basics

While I could talk you through the whole enchilada. Robby Russell over at Planet Argon has already done this. He’s done an excellent job of updating this guide over the years, and it’s usually the first place I go with a new installation. Someday, I’m sure I’ll have it memorized.

Ruby

While you can use the built-in ruby on Snow Leopard, I like to look forward to the day when we’re all 30% faster simply by deploying on Ruby 1.9. I use it in development for that very reason. However, most clients like to use Ruby Enterprise Edition because it’s so efficient and stable. Therefore, we need to switch between versions easily. Enter the Ruby Version Manager.

>sudo gem install rvm

RVM not only keeps your system clean, but it allows for easy switching between versions of ruby.

For most cases I like to use the 1.9.3 as my default ruby.

>rvm install 1.9.3
>rvm use 1.9.3 --default

But I also like to have Ruby 2.0 around for the speed.

>rvm install 2.0

Gems

Make sure rubygems is up to date.

>gem update --system

Edit your ~/.gemrc file to contain something like the following:

---
gem: --no-ri --no-rdoc
:benchmark: false
:backtrace: false
:update_sources: true
:verbose: true
:bulk_threshold: 1000
:sources:
- http://gems.rubyforge.org

The most important reason to do this is for that second line. It stops gem from installing the ri and rdoc documentation which I never use anyway. This speeds up gem installation considerably.

Since we’re using RVM which keeps separate gem directories for each version of Ruby, you’ll need to install a few gems. (Even if you went through this from Robby’s instructions)

The most basic gems first:

>gem install rails mongrel

and then the gems for the DB:

>gem install pg

and some convience gems:

>gem install hirb wirble

And some optional but useful gems:

>gem install haml compass thin cheat

Haml is a huge HTML productivity booster.
Compass (and Blueprint) is a huge CSS productivity booster.
Thin is a super-fast webserver for testing and such (doesn’t tail the log like mongrel or webrick).
Cheat is an awesome command-line tool.

If I had a nickle for everytime i’ve typed:

>cheat strftime

I’d be rich.

Tweaks

Put a file in your home directory called .irbrc and have it include the following:

require 'irb/completion'
 
# History
#require 'irb/ext/save-history'  #wirble does history
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
 
# Wirble is a plugin to colorize your irb, it's installed from a gem (gem install -y wirble)
require 'rubygems'
begin
  # load wirble
  require 'wirble'
 
  # start wirble (with color)
  Wirble.init
  Wirble.colorize
rescue LoadError => err
  warn "Couldn't load Wirble: #{err}"
end
 
ARGV.concat ["--readline", "--prompt-mode", "simple"]
 
# autoindent of code while typing it
IRB.conf[:AUTO_INDENT]=true
 
# Log to STDOUT if in Rails && use HIRB if available
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
  begin
    require 'hirb'
    Hirb.enable
  rescue LoadError => err
    warn "No Hirb: #{err}"
  end
  require 'logger'
  RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end
 
# Easily print methods local to an object's class
class Object
  def local_methods
    (methods - Object.instance_methods).sort
  end
end
 
# copy a string to the clipboard
def pbcopy(string)
  `echo "#{string}" | pbcopy`
  string
end

This turns on Hirb/Wirble for the ruby console as well as colorization, history and redirecting the logger to the console for Rails. This is very handy to see what Rails is doing with your database while you’re issuing commands in the console.

And lastly, some handy Bash aliases that really help speed up Rails work.

# Rails 1.0+
alias ss='script/server'
alias sc='script/console'
alias sg='script/generate'
alias sp='script/plugin'
alias tdl='tail -f log/development.log'
# Rails 2.0+
alias sdb='script/dbconsole'
# Rails 3.0+
alias sr='script/rails'
# Passenger
alias rr='touch tmp/restart.txt'
 
# Useful Git aliases
alias gst='git status'
alias gl='git pull'
alias gp='git push'
alias gdm='git diff | mate'
alias gc='git commit -v'
alias gcam='git commit -v -a -m'
alias gb='git branch --color'
alias gba='git branch --color -a'
alias gaa='git add .'
alias gco='git checkout'
alias gsp='git svn dcommit'
alias gsl='git svn rebase'
alias gsf='git svn fetch'

Appendix A: Use the OS X Dictionary for Rails Documentation.

Priit Haamer created a OS X dictionary file of the Rails API documentation which is oh so handy. I use it constantly as it’s faster that firing up a browser and works anywhere (with or without interweb access).

Conclusion

I hope this helps you get optimized with your Rails development. Please let me know if you have additions or need clarification on some of the items here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.