Ruby is an object oriented programming language where a core concept is providing a good developer experience. It aims to do this by providing a wide range of features, good readability and following the Principle of Least Astonishment.
It is an interpreted language, and thus does not need a compilation step. It’s known for its flexibility in both syntax and the language itself. This can allow developers to write readable, declarative code. These factors tend to make Ruby a somewhat slow performing language compared to others - however Rubyists argue that Ruby is fast enough.
Ruby is a widely used language within the Government Digital Service, with many projects using Ruby on Rails and a number using Sinatra.
The main and reference implementation of Ruby is known as MRI (Matz’s Ruby Interpreter) and is the focus of this document.
At the time of writing, using the current stable version of Ruby 2 is a great choice to work with the language.
Typically a Ruby developer may be working with a number of projects that may use different versions of Ruby thus most developers will use a tool such as rbenv or rvm to allow them to run multiple versions of Ruby on their machine.
It is unusual to need to use a Ruby version before 2.0 and there is little support for Ruby versions below 1.9.
Setting up on macOS
macOS ships with a version of Ruby pre-installed, you can run a Ruby script
$ /usr/bin/ruby script.rb or enter the REPL with
$ /usr/bin/irb where you can execute Ruby code:
2.2.3 :001 > puts "Hello World" Hello World => nil
Setting up rbenv with Homebrew
rbenv makes it easy to use newer versions of Ruby than the pre-installed version on MacOS and allows different versions of Ruby for different projects.
Homebrew is a very commonly used package manager for macOS that will build and install software.
$ brew install rbenv
Install ruby-build, which is a rbenv plugin to add the ability to build ruby versions:
$ brew install ruby-build
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
If you use zsh, update
Update your current shell environment:
$ source ~/.bash_profile
Install your chosen version of Ruby:
$ rbenv install 2.3.1
Specify that as the version you’d like to use for this directory/project:
$ rbenv local 2.3.1
And if all has gone to plan running
$ ruby -v should output the version you
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin14]
Working with Ruby Projects
Ruby projects regularly require dependencies, the de-facto method these are
defined and distributed is through Rubygems and
Bundler. An applications dependencies are installed by
bundle install from the root directory of an application.
Typically the dependencies of the project provide executable files to run
aspects of a project. Eg a Rails application can be run with
bundle exec rails --server. It is common practice to define how to run a Ruby
application through a README file in the root of the project.
Tasks in a Ruby project will typically be run using Rake which, by convention, will usually run the tests of an application if it is not provided any arguments.
Ruby Test Frameworks
Testing is a popular topic among Rubyists with different opinions and approaches. Concepts such as Test-Driven Development (TDD) and Behaviour-Driven Development (BDD) are popular, as are simpler approaches.
One of the most widely used tools for testing in Ruby is RSpec. RSpec employs a natural language approach to describing tests and their expected outcome. Well constructed RSpec tests can be very readable in both their source code and in the output of running the tests.
Minitest is a testing framework which is installed as part of Ruby and is considered a successor to Test::Unit - the other (and original) test framework that was installed with Ruby. Minitest has an emphasis on fast and simple testing, although it provides a number of libraries that can make it very feature rich.
Another commonly used tool for testing in Ruby is Cucumber, which enables tests to be written and/or understand by non technical people. It is argued that Cucumber is not a testing tool as such and more a collaboration tool in that it enables those that define software a means to specify it’s requirements, and those that deliver software to demonstrate the software meets those requirements.
A popular tool for testing web applications in Ruby is Capybara, which can be used in conjunction with most Ruby testing frameworks. Capybara provides the means to specify a users actions on a web application and will interface with browser software to action them.