Overview

1 Bootstrapping your Ruby literacy

This opening chapter aims to quickly bootstrap your Ruby literacy. It frames the language in three interlocking layers—the core language (syntax, semantics, and design), the extensions and libraries that ship with Ruby (plus ways to add your own), and the command-line tools you use to run and support your programs. Along the way, it promises not just orientation but hands-on fluency: enough working knowledge to read, write, and run real Ruby code confidently as you move into the rest of the book.

You begin with practical setup and a syntax “survival kit”: installing Ruby (often via a version manager), choosing a plain-text editor, and using irb to experiment interactively. The chapter surveys identifiers (locals, instance/class/global variables, constants, keywords, method names) and clarifies Ruby’s object model: everything is an object, you send messages with the dot operator, methods may take arguments (parentheses often optional), and self is the default receiver for bareword calls. It then walks through writing, checking, and running programs with ruby, including useful switches (-c, -w), and evolves a simple Celsius-to-Fahrenheit converter across iterations—improving output formatting, accepting keyboard input, and performing file I/O for reading and writing data. Throughout, you see how to think in objects and classes while appreciating Ruby’s flexibility and idioms.

Next, the chapter orients you to the Ruby installation itself and to loading code. Using RbConfig, you learn where executables and libraries live (standard library directories, architecture-specific extensions, and site/vendor locations), and how load, require, and require_relative find and bring features into your program via the load path. It introduces RubyGems and Bundler for installing and pinning dependencies, and spotlights the toolchain you’ll use daily: the ruby interpreter and its common switches (-e, -v/--version, -h/--help, -r, -l), irb for rapid exploration, rake for task automation, gem/bundle for package management, and rdbg for interactive debugging with breakpoints and step-by-step execution. By the end, you have a practical map of Ruby’s language, libraries, and tools—and the muscle memory to explore further.

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 camel-cased (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.

FAQ

What’s the difference between “Ruby”, “ruby”, and “RUBY”?Ruby is the programming language. ruby is the interpreter executable that runs your code. RUBY isn’t an acronym and shouldn’t be written in all caps.
How should I install Ruby and manage multiple versions?On macOS/Linux, use a version manager like rbenv, RVM, asdf, or chruby. On Windows, use RubyInstaller. Version managers let you install and switch Ruby versions safely (the chapter references Ruby 3.4.5).
What is irb and how do I use it effectively?irb is Ruby’s interactive console. Start it with irb (try irb --simple-prompt for cleaner prompts), type Ruby expressions, and see results immediately. Exit with exit or Ctrl-D. irb shows multi-line prompts when an expression isn’t complete.
When should I use puts vs print vs p?puts prints and appends a newline if needed; print prints without a newline; p prints the object’s inspect string (handy for debugging). Example: puts "Hi", print "Hi", p "Hi".
How do I read input and convert it to numbers?Use gets to read a line (returns a String, often ending with \n), then convert with to_i (or to_f for floats). Example: c = gets; f = c.to_i * 9 / 5 + 32.
What kinds of identifiers does Ruby have and how are they named?- Variables: local (name), instance (@name), class (@@name), global ($name)
- Constants: start with uppercase (FirstName, FIRST_NAME)
- Keywords: reserved words (e.g., def, class, if)
- Method names: like locals, can end with ?, !, or =. Prefer snake_case for multiword names.
What does the dot operator do, and what are “messages” vs “methods”?The dot sends a message (method name) to an object: "100".to_i calls to_i on the String. Thinking in messages highlights that objects may handle unknown messages (e.g., via method_missing). Parentheses around arguments are usually optional. Barewords like puts "Hi" call a method on the default receiver, self.
How do I split code across files and load libraries?- load "file.rb": loads every time it’s called (use the full filename).
- require "feature": loads once; omit extension (works for .rb and compiled extensions).
- require_relative "path/feature": resolves relative to the file’s directory.
The load path is $:; Ruby searches those directories for requires.
How do I run a Ruby program and check it for errors?Run with ruby prog.rb. Check syntax and warnings with ruby -cw prog.rb. Useful switches include -e (run code literal), -l (line mode), -rname (require a feature), -v/--version (version/verbose), and -h/--help (switch help). Switches can be combined (e.g., -cw).
How do I install and use gems, and what does Bundler do?Install with gem install gemname, then require "gemname" in code. You can select a specific version with gem "name", "x.y.z" before requiring. Use Bundler to declare dependencies in a Gemfile and run bundle so collaborators install the same gem versions.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Well-Grounded Rubyist, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • The Well-Grounded Rubyist, Fourth Edition ebook for free