develop with

Getting Started

Ruby is a runtime language which means it does not have to be precompiled like Java, C, or Go. Ruby requires a virtual machine (VM) to run the language. You can use either Jruby (JVM based runtime) or the default Ruby VM. The majority of usage for ruby is for web applications using Rails or Sinatra. Although the devops community has adopted it extensively for configuration and provisioning in Puppet and Chef.

Setup

To install the default ruby runtime, you’ll need to use a package manager for OS X and Ubuntu or an Installer for Windows. This is unless you use a version manager as outlined in the below section.

Install Windows: Download the RubyInstaller and follow the instructions.

Install Ubuntu: The package will actually install the latest ruby version with the compatibility for ruby 1.9.1.

 sudo apt-get install ruby1.9.1

Install OS X: In order to install on OS X, you’ll need to install brew (aka homebrew).

 brew install ruby

For more installation options see the Ruby Downloads page.

Version Managers

Its best practice to run your ruby development out of a version manager. A version manager isolates your applications gemsets from other applications giving you a better feel of how that will run in production. There are 2 popular choices RVM and RbEnv.

RVM Setup

To install, run the following:

curl -L https://get.rvm.io | bash -s stable

Add a ruby environment and set a default ruby version:

rvm install 2.0.0-p247
rvm --default 2.0.0-p247

Note: If you are using Xcode's LLVM/Clang compiler on OS X, you may need to install Ruby via rvm install 2.0.0-p247 --with-gcc=clang

To install global gems such as the bundler you’ll need to set the global gemset, by running the following:

rvm 2.0.0-p247@global
gem install bundler --pre

The @global represents the gemset in which you are switching too, this could be your application’s gemset by having @myapplication. The gemset needs to be created in order to be used, a quick way to do this would be to run rvm use 2.0.0-p247@myapplication --create. This will create and set the gemset to be used within the shell (command line) context.

To set the ruby version permanently in your application, create a .ruby-version file in your application directory containing 2.0.0-p247. You’ll also need a .ruby-gemset file that contains the reference to the gemset, ie. myapplication.

Bundler

Most applications you’ll see written in ruby take advantage of the functionality in the Bundler. The Bundler provides a way to create a Gemfile that lists all your dependencies and will install them for you by running gem install in your application directory. It is a very convenient way to work on a Ruby project with a team. When gem install is run it will create a Gemfile.lock file which indicates what the last set of gems install as part of the project. This is typically checked into the version control with the Gemfile.

Example of a Gemfile:

source 'https://rubygems.org'
gem 'rack', '~>1.1'
gem 'rspec', :require => 'spec'

Next: Basics

comments powered by Disqus

Want to see a topic covered? create a suggestion

Get more developer references and books in the developwith store.