Overview

1 Bootstrapping your Ruby literacy

The chapter introduces Ruby by giving readers a practical foundation for getting started quickly and comfortably. It emphasizes that Ruby is both a language and, lowercase ruby, the interpreter program that runs code. The opening approach is deliberately broad: learn enough syntax, tools, and workflow to begin writing, checking, and running simple programs while also gaining a sense of how Ruby is organized as a language and ecosystem.

A large part of the chapter is a survival kit for Ruby literacy. It reviews basic operations, input and output, conditionals, special values, comments, and the main kinds of identifiers such as variables, constants, keywords, and method names. It also explains Ruby’s object-based model, where messages are sent to objects through method calls, sometimes with a receiver and sometimes through the default object self. These ideas are reinforced through small temperature-conversion examples that show how to write a file, run it through the interpreter, check for syntax errors, use interactive testing, and handle keyboard and file input and output.

The later parts of the chapter expand beyond syntax into the practical environment around Ruby. Readers are shown how to inspect the Ruby installation, understand the locations of standard libraries, C extensions, site and vendor directories, and gems, and how Ruby loads libraries with load, require, and require_relative. The chapter then surveys the most useful built-in command-line tools, especially irb for experimentation, rake for tasks, gem and Bundler for package management, and rdbg for debugging. Overall, the chapter’s goal is to make readers self-sufficient enough to explore Ruby code, understand where it comes from, and begin using the language’s ecosystem productively.

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.

FAQ

```html
What is the difference between Ruby and the ruby program?Ruby is the programming language, while ruby is the command-line program that reads and runs Ruby code.
What are the three main levels of the Ruby landscape introduced in this chapter?The chapter groups Ruby into three levels: the core language, extensions and libraries, and command-line tools.
Why is irb so useful when learning Ruby?irb lets you type Ruby code interactively, immediately see results, and experiment with snippets without creating a file first.
What does the --simple-prompt option do in irb?It changes irb’s prompt to a shorter, easier-to-read form, which makes examples and experimentation less cluttered.
What are the main differences between print and puts?puts automatically adds a newline if needed, while print outputs exactly what it is given and leaves the cursor where it is.
How do I tell Ruby to check a file for syntax errors without running it?Use the -c flag, often combined with -w as ruby -cw filename.rb, to check syntax and show warnings.
What are Ruby identifiers, and what kinds are covered in this chapter?Identifiers are names used in Ruby, including variables, constants, keywords, and method names. The chapter also breaks variables into local, instance, class, and global variables.
How do load, require, and require_relative differ?load loads a file every time it is called, require loads a feature only once and searches the load path, and require_relative loads a file relative to the current file’s directory.
What is the load path in Ruby?The load path is the list of directories Ruby searches when loading files or features. You can inspect it through $LOAD_PATH or $:.
What does the rake tool do?rake is a Ruby-based task-management utility used to define and run tasks from a Rakefile, similar in spirit to make.
```

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