Preface – The Ruby Workshop




This section briefly introduces the coverage of this book, the technical skills you'll need to get started, and the software requirements required to complete all of the included activities and exercises.

About the Book

The beauty of Ruby is its readability and expressiveness. Ruby hides away a lot of the complexity of programming, allowing you to work quickly and ‘do more' with fewer lines of code. This makes it a great programming language for beginners, but learning any new skill can still be a daunting task. If you want to learn to code using Ruby, but don't know where to start, The Ruby Workshop will help you cut through the noise and make sense of this fun, flexible language.

You'll start by writing and running simple code snippets and Ruby source code files. After learning about strings, numbers, and booleans, you'll see how to store collections of objects with arrays and hashes. You'll then learn how to control the flow of a Ruby program using boolean logic.

The book then delves into OOP and explains inheritance, encapsulation, and polymorphism. Gradually, you'll build your knowledge of advanced concepts by learning how to interact with external APIs, before finally exploring the most popular Ruby framework – Ruby on Rails – and using it for web development.

Throughout this book, you'll work on a series of realistic projects, including simple games, a voting application, and an online blog. By the end of this Ruby book, you'll have the knowledge, skills and confidence to creatively tackle your own ambitious projects with Ruby.

About the Chapters

Chapter 1, Writing and Running Ruby Programs, introduces you to writing and running simple snippets interactively as well as writing and running Ruby source code files.

Chapter 2, Ruby Data types and Operations, will teach you how to work with more complex data types such as arrays and hashes.

Chapter 3, Program Flow, informs you about the different options for controlling how a Ruby program flows: where it branches based on Boolean logic, where it switches tracks based on different cases, and where it circles back on itself to do repetitive work.

Chapter 4, Ruby Methods, builds your understanding of how to construct your own methods and master the concise and elegant syntax that Ruby offers for doing so.

Chapter 5, Object-Oriented Programming with Ruby, familiarizes you with Object-Oriented (OO) programming concepts with Ruby. You will learn how to organize code in a way that sets us up to write larger and more complex applications. We'll also begin looking at a common practice known as Test-Driven Development (TDD), which helps us build better code by getting us to think about our tests first.

Chapter 6, Modules and Mixins, will let you dive deeper into Ruby and OO programming concepts and learn about inheritance, encapsulation, and polymorphism. We will implement these using powerful Ruby language features known as modules and mixins. These will allow us to organize code and teach us deeper concepts of the Ruby object model.

Chapter 7, Introduction to Ruby Gems, focuses on importing data from external sources, processing it using our application and outputting the data in human-readable formats. We'll dive into Ruby's excellent package management system, RubyGems, how to read and write files to the filesystem, and finally how to neatly encapsulate our newly learned service-based code with service classes.

Chapter 8, Debugging with Ruby, instructs you on fundamental logging and debugging concepts and how we debug with Ruby. We'll cover different methods for different purposes and learn real-world techniques that can save you time and effort when it comes to solving problems in both development and production.

Chapter 9, Ruby Beyond the Basics I, teaches you about techniques and concepts such as blocks, procs, and lambdas. We will also learn about a very powerful concept of Ruby called metaprogramming, which is essentially code that writes code.

Chapter 10, Ruby Beyond the Basics II, teaches you how to interact with external APIs. Our code will consume a public API and parse the response. We will then create a reusable module out of this Ruby code and publish it as a Ruby gem.

Chapter 11, Introduction to Ruby on Rails I, introduces you to the most popular Ruby framework, Ruby on Rails. We will learn about the Model-View-Controller (MVC) architecture and how to organize code in Ruby on Rails. When learning about the MVC architecture, we'll see how to keep a fat model and a thin controller. Lastly, we will create a simple Create, Read, Update, and Delete (CRUD) web application with Ruby on Rails.

Chapter 12, Introduction to Ruby on Rails II, continues to build our Rails application and you will learn about REST principles and other fundamental concepts of Ruby on Rails such as routing, the asset pipeline, and ORM (Object Relational Mapping) with ActiveRecord. Finally, we will deploy our Rails application on the server with Heroku.


Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We use puts to print a string".

Words that you see on the screen, for example, in menus or dialog boxes, also appear in the text like this: "Both of them are inherited by the Integer class".

A block of code is set as follows:

[1,2,3].each do |i|

  puts "My item: #{i}"


New terms and important words are shown like this: "With metaprogramming, you can write methods and classes at runtime, which helps us to maintain a Don't Repeat Yourself (DRY) and maintainable code base".

Long code snippets are truncated and the corresponding names of the code files on GitHub are placed at the top of the truncated code. The permalinks to the entire code are placed below the code snippet. It should look as follows:


1  module Address

2    attr_accessor :address_line1, :address_line2, :city, :state,:postal_code, :country  

Before You Begin

Each great journey begins with a humble step. Our upcoming adventure with Ruby programming is no exception. Before we can do awesome things using Ruby, we need to be prepared with a productive environment. In this small note, we shall see how to do that.

Installing Ruby 2.6

All code in this book is Ruby 2.x-compatible. However, we have specifically used Ruby 2.6 in our lab environment for all of the book content. We recommend using Ruby 2.6 to avoid any issues completing the chapter.

Please note that the bundler gem, which is used in various chapters of the book, is included within Ruby 2.6, but earlier versions of Ruby will require a manual install by simply running the following code:

gem install bundler

gem install rails

You can use any editor of your choosing to work with Ruby. However, our editor of choice is Visual Studio Code ( combined with the Ruby language extension (

For Ruby on Rails, you can simply install the extension for Rails on the Visual Studio Code Editor interface. Make sure you have installed all gems using the bundler so that the enabled extension works. If you have any issues or questions about installation, please email us at

Installing the Code Bundle

Download the code and relevant files from GitHub at and place them in a new folder called C:\Code. Refer to these code files for the complete code bundle.