Overview

1 Bootstrapping your Ruby literacy

This chapter sets out to quickly bootstrap your Ruby literacy by giving you just enough syntax, tooling, and mental models to work productively. It frames the language ecosystem in three interlocking layers: the core language (design principles, syntax, and semantics), the extensions and libraries that expand its capabilities, and the command-line tools you’ll use every day. Along the way it establishes Ruby’s object-centric worldview—everything is an object that responds to messages (methods), with self as the implicit receiver—and orients you to the main identifier types (locals, instance and class variables, globals, constants, keywords, and method names) and the supportive role of classes in creating, but not strictly limiting, object behavior.

The chapter then walks you through hands-on bootstrapping: installing Ruby (often via a version manager), setting up a plain‑text editor and a working directory, and using irb to probe the language interactively. A compact “syntax survival kit” covers arithmetic, assignment and comparisons, screen output with puts/print/p, keyboard input with gets, conditionals, special values (true/false/nil), comments, and self. You immediately apply these ideas in a small, iterative Celsius‑to‑Fahrenheit program: write and save files, check syntax and warnings with ruby -cw, run the code, improve output formatting, accept keyboard input, and read from or write to files—while learning to interpret multi‑line expressions and error messages as you go.

Finally, you tour the Ruby installation and its libraries: where standard Ruby files live (rubylibdir), where compiled C extensions reside (archdir), and where site/vendor code and gems are found. You learn how Ruby locates and loads code via the load path ($:), and how to bring features into your programs with load, require, and require_relative, including requiring standard libraries like uri and working with gems. The chapter caps off with core tooling: ruby interpreter switches (-c, -w, -e, -l, -r, -v, --version, -h), productive irb options, rake for task automation with namespaced Rakefiles, the gem and bundler workflow for dependency management, and rdbg for interactive debugging. Together, these concepts and tools ground you for deeper exploration of Ruby.

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” the language and the “ruby” executable?Ruby is the programming language. ruby (lowercase) is the interpreter program you run at the command line to execute Ruby code. Ruby isn’t an acronym, so writing RUBY in all caps is incorrect.
What’s the recommended way to install Ruby on macOS, Linux, and Windows?- macOS/Linux: Use a version manager such as RVM, rbenv, asdf, or chruby. They make installing, switching, and isolating Ruby versions easy and safe.
- Windows: Use RubyInstaller.
Create a dedicated folder for your practice code and use a plain-text editor. The chapter references Ruby 3.4.5.
What is irb and how do I use it effectively?irb is the interactive Ruby console. Type Ruby expressions and see results immediately.
- Start: irb or irb --simple-prompt for a cleaner prompt.
- Example: 100 + 32 returns 132.
- Multiline input: irb prompts you to finish incomplete expressions.
- Exit: type exit or press Ctrl-D.
What’s the difference among print, puts, and p?- puts prints a value and appends a newline if one isn’t present.
- print prints exactly what you give it without adding a newline.
- p prints an inspect-style representation (useful for debugging).
Tip: Use puts when you want a newline automatically; use print to control layout; use p to see internal representations.
How do Ruby identifiers work (variables, constants, keywords, methods)?- Local variables: lowercase or underscore start (example: first_name, count1).
- Instance variables: start with @ (example: @age).
- Class variables: start with @@ (example: @@running_total).
- Global variables: start with $ (example: $stdin, $LOAD_PATH).
- Constants: start with an uppercase letter (example: String, FIRST_NAME).
- Keywords: reserved words like def, class, if, __FILE__.
- Method names: like locals, may end with ?, !, or = (example: empty?, upcase!, total=). The Ruby convention is snake_case for methods and locals.
How do method calls and messages work in Ruby?Everything is an object. The dot sends a message (method name) to a receiver: "100".to_i sends to_i to the string "100". Parentheses around arguments are often optional: "100".to_i(9) converts from base 9. Bareword calls like puts "Hello" are methods sent to the default receiver self. If a method isn’t found, Ruby can intercept the message (for example, via method_missing).
How do I load external code: load vs require vs require_relative? What’s the load path?- load "file.rb" reads and executes a file every time it’s called; you usually include the .rb extension.
- require "feature" loads a “feature” once (caches it). Omit extensions; Ruby will find .rb or compiled extensions (.so/.dll/.bundle).
- require_relative "path/to/feature" resolves relative to the calling file’s directory (handy for multi-file projects).
- Load path ($:) is the list of directories Ruby searches for require; the current directory isn’t automatically on it for require (it is effectively considered for load).
How do I check a Ruby file for errors and what are useful interpreter switches?- Check syntax and warnings: ruby -cw file.rb (doesn’t run the program).
- Run a file: ruby file.rb
- Handy switches:
- -e 'code' executes inline code.
- -l forces a newline after each output line.
- -rname requires a feature before running (for example, -ruri).
- -v prints version and runs in verbose mode; --version prints version and exits.
- -h or --help lists all switches.
You can combine switches (for example, -ve).
How do I read input from the keyboard and from files, and write to files?- Keyboard: gets reads a line (including the trailing newline). Convert with to_i or to_f before arithmetic.
- Read a file: num = File.read("temp.dat")
- Write a file:
fh = File.new("temp.out", "w")
fh.puts value
fh.close
Use "w" to write (truncates), "a" to append, "r" to read.
How do I install and use libraries with RubyGems and Bundler?- Install a gem: gem install gemname (installs dependencies too).
- Use it: require "gemname" in your code.
- Pin a version at runtime (optional): gem "gemname", "1.2.3" before require.
- Manage per-project dependencies with Bundler:
- Create a Gemfile listing source and gems (for example, gem "colorize", "1.1.0").
- Run bundle to install.
- Then require gems normally in your code.

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
$399.99
only $33.33 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
$399.99
only $33.33 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