Part 1. Ruby foundations
The goal of this part of the book is to give you a broad but practical foundation layer on which to build, and to which to anchor, the further explorations of Ruby that follow in parts 2 and 3. Weâll start with a chapter on bootstrapping your Ruby literacy; after working through that first chapter, youâll be able to run Ruby programs comfortably and have a good sense of the layout of a typical Ruby installation. Starting with chapter 2, weâll get into the details of the Ruby language. Ruby is an object-oriented language, and the sooner you dive into how Ruby handles objects, the better. Accordingly, objects will serve both as a way to bootstrap the discussion of the language (and your knowledge of it) and as a golden thread leading us to further topics and techniques.
Objects are created by classes, and in chapter 3 youâll learn how classes work. The discussion of classes is followed by a look at modules in chapter 4. Modules allow you to fine-tune classes and objects by splitting out some of the object design into separate, reusable units of code. To understand Ruby programsâboth your own and othersââyou need to know about Rubyâs notion of a current default object, known by the keyword self. Chapter 5 will take you deep into the concept of self, along with a treatment of Rubyâs handling of variable visibility and scope.
In chapter 6, the last in this part of the book, youâll learn about control flow in Rubyprogramsâthat is, how to steer the Ruby interpreter through conditional (if) logic, how to loop repeatedly through code, and even how to break away from normal program execution when an error occurs. By the end of chapter 6, youâll be thinking along with Ruby as you write and develop your code.
The title of this part is âRuby foundations,â which obviously suggests that whatâs here is to be built on later. And thatâs true. But it doesnât mean that the material in part 1 isnât important in itself. As youâll see once you read them, these six chapters present you with real Ruby techniques, real code, and information youâll use every time you write or execute a Ruby program. Itâs not the âfoundationsâ because youâll learn it once and then ignore it, but because thereâs so much more about Ruby yet to follow!
Chapter 1. Bootstrapping your Ruby literacy
This chapter covers - A Ruby syntax survival kit
- Writing, saving, running, and error-checking programs
- A tour of the Ruby installation
- The mechanics of Ruby extensions
- Rubyâs command-line tools such as irb and rake
This book will give you a foundation in Ruby, and this chapter will give your foundation a foundation. The goal of the chapter is to bootstrap you into the study of Ruby with enough knowledge and skill to proceed comfortably into what lies beyond.
Weâll look at basic Ruby syntax and techniques and at how Ruby works: what you do when you write a program, how you get Ruby to run your program, and how you split a program into more than one file. Youâll learn several of the switches that alter how the Ruby interpreter (the program with the name ruby, to which you feed your program files for execution) acts, as well as how to use some important auxiliary tools designed to make your life as a Rubyist easier and more productive.
The chapter is based on a view of the whole Ruby landscape as being divided into three fundamental levels:
- Core language: design principles, syntax, and semantics
- Extensions and libraries that ship with Ruby, and the facilities for adding extensions of your own
- Command-line tools that come with Ruby, with which you run the interpreter and some other important utilities
Itâs not always possible to talk about these three levels in isolationâafter all, theyâre interlocking parts of a single systemâbut weâll discuss them separately as much as possible in this chapter. You can, in any case, use the three level descriptions as pegs to hang subtopics on, wherever theyâre introduced.
Ruby, ruby, and ... RUBY?! Ruby is a programming language. We talk about things like âlearning Ruby,â and we ask questions like, âDo you know Ruby?â
The lowercase version, ruby, is a computer program. Specifically, itâs the Ruby interpreter, the program that reads your programs and runs them. Youâll see this name used in sentences like âI ran ruby on my file, but nothing happened,â or âWhatâs the full path to your ruby executable?â
Finally, thereâs RUBYâor, more precisely, there isnât. Ruby isnât an acronym, and itâs never correct to spell it in all capital letters. People do this, as they do (also incorrectly) with Perl, perhaps because theyâre used to seeing language names like BASIC and COBOL. Ruby isnât such a language. Itâs Ruby for the language, ruby for the interpreter.
Nor does this first chapter exist solely in the service of later chapters. It has content in its own right: youâll learn real Ruby techniques and important points about the design of the language.
1.1. Basic Ruby language literacy
The goal of this section is to get you going with Ruby. It takes a breadth-first approach: weâll walk through the whole cycle of learning some syntax, writing some code, and running some programs.
1.1.1. Installing Ruby and using a text editor
Though youâre free to install and compile Ruby from source from www.ruby-lang.org, itâs far more common for Rubyists using macOS or Linux to install versions of Ruby using a version manager. The most popular version managers are RVM (https://rvm.io), rbenv (https://github.com/rbenv/rbenv), and chruby (https://github.com/postmodern/chruby). Windows users are encouraged to use the RubyInstaller (https://rubyinstaller.org/). All version managers are free and all provide a safe and easy way to download and run Ruby. This book references Ruby version 2.5.1.
Youâll also need a text editor (any editor you like, as long as itâs a plain-text editor and not a word processor) and a directory (a.k.a. a folder) in which to store your Ruby program files. You might name that directory rubycode or rubysamplesâany name is fine. Keep it separate from other work areas so that you can keep track of your practice program files.
The interactive Ruby console program (irb), your new best friend The irb utility ships with Ruby and is the most widely used Ruby command-line tool other than the interpreter itself. After starting irb, you type Ruby code into it, and it executes the code and prints out the resulting value.
Type irb at the command line and enter sample code as you encounter it in the text. For example:
>> 100 + 32 => 132
Having an open irb session means you can test Ruby snippets at any time and in any quantity. Most Ruby developers find irb indispensable, and youâll see a few examples of its use as we proceed through this chapter.
To exit from irb normally, you can type exit. On many systems, Ctrl-D works too.
The irb examples youâll see in this book use a command-line option that makes irb output easier to read:
irb --simple-prompt
If you want to see the effect of the --simple-prompt option, try starting irb with and without it. As youâll see, the simple prompt keeps your screen a lot clearer. The default (nonsimple) prompt displays more information, such as a line-number count for your interactive session; but for the examples weâll look at, the simple prompt is sufficient.
Because irb is one of the command-line tools that ship with Ruby, itâs not discussed in detail until section 1.4.2. Feel free to jump to that section and have a look; i...