A Student's Guide to Software Engineering Tools & Techniques »

Introduction to Ruby

Authors: Wilson Kurniawan

Ruby Overview

Ruby is a dynamic, reflective, object-oriented, general-purpose programming-language, designed and developed by Yukihiro "Matz" Matsumoto in 1990s. It is widely used across many fields of computer science and software engineering, and its most prevalent use is in web development.

Ruby boasts high programmer productivity with its concise, elegant, and human-readable syntax, which is widely agreed to outweigh the loss in execution speed (as compared to compiled languages such as C and Java) unless the latter is critical.

Often people, especially computer engineers, focus on the machines. They think, "By doing this, the machine will run faster. By doing this, the machine will run more effectively. By doing this, the machine will something something something." They are focusing on machines. But in fact, we need to focus on humans, on how humans care about doing programming or operating the application of the machines. We are the masters. They are the slaves.

Yukihiro Matsumoto, creator of Ruby language

Popular websites running on Ruby: GitHub, Airbnb, Twitter, Shopify.

Getting Started

The official website of Ruby will have everything you need from release notes to downloads to documentations.

To install Ruby, go to the download page of the above website and follow the instructions. If you're a Mac user, you don't need to do anything; Macs come with Ruby pre-installed!

For beginners and intermediate users, tutorialspoint has great introductory Ruby tutorials, and rubylearning.com is another great place. We will explore some of the more prominent language features of Ruby.

Everything is an Object

Ruby has no concept of "primitives". Anything that can be assigned to a variable is an object. Even numbers and boolean values true and false are objects.

5.times do
  puts "Hello world!"
end

# Hello world!
# Hello world!
# Hello world!
# Hello world!
# Hello world!

As such, it becomes very easy to introduce new functions to those "primitives":

class Fixnum # integers in Ruby belong to this class
  def next()
    return self + 1
  end
end

7.next()
#  => 8

Readability is King

Ruby is simple in appearance, but is very complex inside, just like our human body.

Yukihiro Matsumoto

Some languages attempt to make their codes as close to pseudocode as possible, but Ruby takes it to another level: Ruby code flows almost as smoothly as an English literary text.

for num in 0..5
  puts "I am counting #{num}!"
end

unless number.even?
  puts "Number is odd!"
end

["chocolate", "strawberry", "vanilla"].each { |flavour|
  make_ice_cream(flavour)
}

No, you're not reading an English poem. You're reading Ruby!

Functional Programming is Encouraged

For those familiar with higher-order functions, Ruby supports map, fold-left, and filter.

# Map
[1, 2, 3, 4, 5].map { |n| n * n }
#  => [1, 4, 9, 16, 25]

# Fold-left
[1, 2, 3, 4, 5].inject(0) { |sum, n| sum + n }
#  => 15

# Filter
[1, 2, 3, 4, 5].select { |n| n.even? }
#  => [2, 4]

Joel McCracken makes an excellent short presentation on how functions are treated in Ruby.

Object-Oriented Programming is Possible, Too

class Circle
  def initialize(r)
    @radius = r
  end

  def get_radius
    return @radius
  end

  def get_area
    return Math::PI * @radius * @radius
  end
end

c = Circle.new(5)
c.get_area
#  => 78.53981633974483

More on this in Object-oriented Ruby tutorial.

Advanced Topics

Ruby Frameworks and DevOps

As with most other languages, tools and frameworks exist for serious Ruby developers and project managers to assist many of their tasks.

  • Frameworks: Ruby on Rails is by far the most popular web application framework for Ruby. Another popular framework is Sinatra.
  • Libraries: Ruby libraries come in form of gems. The complete registry can be found on this website.
  • IDE: Aptana Studio is the favourite IDE for Rails developers. Other alternatives are RubyMine (commercial) and Eclipse with RDT plugin.
  • Task Automation: Rake is the most commonly used automation tool.
  • Static Analysis: RuboCop is the sole leading static analysis tool for Ruby language.

This repository lists down a large collection of Ruby libraries, tools, frameworks, and software.