Overview

1 Bootstrapping your Ruby literacy

This opening chapter sets a practical foundation for Ruby literacy. It frames the language in three interlocking layers—core syntax and semantics, extensions and libraries, and command-line tools—and clarifies the distinction between Ruby the language and ruby the interpreter. It builds identifier fluency (local, instance, class, and global variables; constants; keywords; and method names) and anchors the Ruby mindset around objects and messages: you send messages (method calls) to objects with dot notation, sometimes to the default receiver self. Classes define how objects start life, but objects can gain behavior dynamically; truthiness, comments, and staple control flow round out the “syntax survival kit.”

From there, the chapter bootstraps hands-on skills: installing Ruby, choosing an editor, and using irb to explore code interactively. It walks through writing, saving, checking, and running a small program (a Celsius-to-Fahrenheit converter), illustrating print versus puts, keyboard input with gets, numeric conversion with to_i, and simple file I/O for reading and writing. You learn to interpret syntax errors, leverage interpreter switches (-c, -w, -e, -v, -l, -r), and combine them effectively. The chapter also shows how to structure multi-file programs and explains the load path ($:), RbConfig for discovering install locations, and the differences among load (always reloads), require (loads once, feature-oriented), and require_relative (path-relative loading).

Finally, it surveys the Ruby installation and ecosystem: where standard library code and compiled extensions live (rubylibdir, archdir), where site/vendor code and gems are found, and how RubyGems and Bundler install and manage dependencies (including pinning versions). It highlights everyday tools that ship with Ruby—irb for rapid experimentation, rake for Ruby-native task automation with namespaced tasks, gem/bundle for package management, and rdbg for breakpoint-driven debugging and stepwise execution. Taken together, these concepts and tools equip newcomers to write, run, organize, and extend Ruby programs confidently while navigating the language’s libraries and runtime environment.

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

How should I install Ruby and manage multiple versions?On macOS or Linux, use a version manager such as RVM, rbenv, asdf, or chruby to install and switch Ruby versions safely. On Windows, use RubyInstaller. Version managers make it easy to keep different projects on different Ruby versions (the chapter references Ruby 3.4.5).
Which Ruby interpreter command-line switches are most useful?Common flags: -c (syntax check), -w (warnings), -e (run code given on the command line), -l (line mode), -r name (require a feature before running), -v/--version (show version; -v also runs in verbose mode), -h/--help (help). You can combine them, e.g., ruby -cw file.rb or ruby -ve "puts 2+2".
What is irb, and how do I use it effectively?irb is Ruby’s interactive console. Start it with irb (try --simple-prompt for cleaner prompts). Type Ruby code and see results immediately. Use it to experiment with snippets, test expressions, and inspect objects. Exit with exit or Ctrl-D. If you see a secondary prompt (e.g., ?) it’s waiting for you to finish a multi-line expression.
What core syntax should I know to get started?Essentials: arithmetic (+, -, *, /), assignment (x = 1), comparison (==), keyboard input (gets returns a string), output (puts, print, p), conditionals (if ... else ... end), special values (true, false, nil), and comments (# ...).
What’s the difference between puts, print, and p?puts prints its arguments and adds a newline if one isn’t present. print prints exactly what you pass (no automatic newline). p prints the “inspect” representation of objects (useful for debugging) and adds a newline.
Which identifier types does Ruby use, and how do I recognize them?- Local variables: first_name (lowercase or underscore start).
- Instance variables: @age.
- Class variables: @@running_total.
- Global variables: $stdin, $LOAD_PATH (start with $).
- Constants: String, FIRST_NAME (start with uppercase).
- Keywords: reserved words like def, class, if.
- Method names: like locals, may end with ?, !, or =.
What does “everything is an object” mean in Ruby, and how do method calls work?Numbers, strings, arrays—everything—are objects that respond to messages (methods). You send a message with the dot: "100".to_i sends to_i to a string. Methods can take arguments (parentheses are often optional). If an object doesn’t have a matching method, it can intercept unknown messages via method_missing. Barewords like puts "Hi" are method calls to the current self.
How do I write, run, and syntax-check a Ruby program?Save your code in a .rb file (e.g., c2f.rb). Run it with ruby c2f.rb. Check for syntax errors without running using ruby -cw c2f.rb (reports “Syntax OK” if fine). Prefer puts when you want a newline; use print to avoid an automatic newline.
How do I do basic keyboard and file I/O?- Keyboard: gets reads a line (string, typically ending with \n); convert with to_i or to_f before arithmetic.
- File read: num = File.read("temp.dat").
- File write: fh = File.new("temp.out","w"); fh.puts value; fh.close.
What’s the difference between load, require, and require_relative?load reads and executes a file every time you call it (use the full filename; good for live reloading). require loads a “feature” once (omit extension; works with .rb and compiled extensions). require_relative resolves paths relative to the file doing the requiring (handy within a project). Ruby searches the load path ($:) when requiring.
How do I find Ruby’s installation directories and where tools live?Use rbconfig to query installation paths in an irb session: require "rbconfig", then e.g., RbConfig::CONFIG["bindir"] (command-line tools), "rubylibdir" (standard library), "archdir" (C extensions), "sitedir"/"vendordir" (third-party libs). This helps you see where Ruby, irb, and libraries are installed.

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