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
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.1Install macOS: In order to install on macOS, you’ll need to install brew (aka homebrew).
brew install rubyFor 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 stableAdd a ruby environment and set a default ruby version:
rvm install 2.0.0-p247
rvm --default 2.0.0-p247Note: 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 --preThe @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'Hello World
Create a file called hello.rb:
puts "Hello, Ruby!"Run it:
ruby hello.rbString Processing and Interpolation
Ruby supports double-quoted strings with interpolation and single-quoted strings as literals:
name = "World"
puts "Hello, #{name}!" # => Hello, World!
puts 'Hello, #{name}!' # => Hello, #{name}! (no interpolation)Common string operations:
"hello".upcase # => "HELLO"
"HELLO".downcase # => "hello"
" hello ".strip # => "hello"
"hello".reverse # => "olleh"
"hello world".split # => ["hello", "world"]
"hello".sub("l", "r") # => "herlo"Error Handling
Ruby uses begin/rescue/ensure for exception handling:
begin
result = 10 / 0
rescue ZeroDivisionError => e
puts "Caught an error: #{e.message}"
ensure
puts "This always runs"
endYou can raise your own exceptions:
raise ArgumentError, "Name cannot be nil" if name.nil?Mapping and Filtering
Ruby’s Enumerable module provides powerful collection operations:
numbers = [1, 2, 3, 4, 5]
# Map — transform each element
doubled = numbers.map { |n| n * 2 }
# => [2, 4, 6, 8, 10]
# Select / Filter — keep matching elements
evens = numbers.select { |n| n.even? }
# => [2, 4]
# Reject — remove matching elements
odds = numbers.reject { |n| n.even? }
# => [1, 3, 5]
# Reduce / Inject — accumulate a result
sum = numbers.reduce(0) { |acc, n| acc + n }
# => 15
# Each — iterate without collecting
numbers.each { |n| puts n }Hash transformations work similarly:
hash = { a: 1, b: 2, c: 3 }
hash.transform_values { |v| v * 10 }
# => { a: 10, b: 20, c: 30 }Next: Basics