1 Bootstrapping your Ruby literacy
This chapter aims to quickly bootstrap your Ruby literacy by giving you enough of the language, environment, and tools to move forward confidently. It frames Ruby across three interlocking levels: the core language (objects, messages, methods, and the role of classes), the standard libraries and extensions (both Ruby and C based), and the command-line tools that ship with Ruby. The overall approach blends practical, hands-on exercises with conceptual grounding so you learn not only what Ruby can do, but how and why it does it.
You start by setting up Ruby and a plain‑text editor, then lean on irb to experiment interactively while you pick up essential syntax: arithmetic and assignment, comparisons, input and output, truthiness (true, false, nil), comments, and the difference between print, puts, and p. The chapter teaches you to recognize Ruby’s identifiers at a glance (local, instance, class, and global variables; constants; keywords; method names) and builds a clear mental model of Ruby as “objects receiving messages,” using the dot operator, optional parentheses, and bareword calls on self. With that foundation, you write a small Celsius‑to‑Fahrenheit converter, check it with ruby -c -w, run and refine its output, then extend it to accept keyboard input and to read from and write to files—cementing core I/O patterns you’ll reuse often.
From there, the chapter tours the Ruby installation so you know where things live and how Ruby finds them (RbConfig, rubylibdir, archdir, site and vendor directories, and the gem repository). It explains how to split programs across files and load code with load, require, and require_relative, and how the load path and require’s “load once” behavior work in practice. You also get a practical toolkit: interpreter switches for common workflows (-c, -w, -e, -l, -r, -v, --version, -h), deeper irb usage (history, completion, simple prompts, multi‑line input, and noecho), rake for task automation with cleanly namespaced tasks, and everyday gem and Bundler routines for managing dependencies. The chapter finishes by introducing rdbg for interactive debugging with breakpoints, stepping, and quick variable inspection—rounding out a compact, working starter kit for productive Ruby programming.
Summary
- irb allows you to evaluate expressions and print results. We’ll use it extensively throughout the book.
- Ruby allows us to easily perform arithmetic, assignment, and determine equality.
- print, puts, p, and gets are methods used for basic I/O.
- Local variables (first_name), instance variables (@first_name), class variables (@@first_name), and global variables ($first_name) are distinguished by the character(s) that precede them. Constants start with uppercase letters and Pascal case (FirstName) or all uppercase separate by underscores (FIRST_NAME).
- Everything in Ruby is an object. We send messages to objects by using the . syntax. Bareword calls send to self, and unknown messages are handled via method_missing.
- ri/rdoc, rake, gem, bundle, and rdbg are all useful command line tools that come pre-packaged with Ruby. Spend some time getting to know these tools and what they can do.
The Well-Grounded Rubyist, Fourth Edition ebook for free