liveBook · Manning

Ruby

As explained in The Well-Grounded Rubyist, Fourth Edition

Ruby is a dynamically typed, object-oriented programming language with garbage collection and an emphasis on expressive syntax.

Try `100 + 32` in `irb`: Ruby evaluates the expression and prints `132`. The same language can run code saved in a plain-text file. Use `irb` for quick experiments and the `ruby` interpreter to run a program. section 1.1.1 section 1.1.6

Ruby programs manipulate objects. Assignment such as `x = 1` binds a local variable to an object; it does not make the variable the object. To ask an object to do something, send it a message with dot syntax. A method call without an explicit receiver goes to `self`, the default object in the current execution context. An object's class helps determine which methods it can respond to. section 1.1.2 section 1.5 section 2.1.1

A few small distinctions make Ruby experiments easier to read:

  • `=` assigns; `==` compares and returns `true` or `false`. In a conditional, only `false` and `nil` count as false—not `0` or an empty string. section 1.1.2
  • `puts` adds a newline when needed; `print` leaves the cursor where the output ends; `p` prints an inspection-oriented representation. section 1.1.2
  • If `irb` waits after an incomplete expression, finish it on the same line or the next one. section 1.1.2

Check a file before running it

A Celsius-to-Fahrenheit program with a missing closing parenthesis cannot run: Ruby reports a syntax error, potentially at the end of the file after waiting for the expression to close. Run `ruby -cw c2f.rb` to check syntax and request warnings, then `ruby c2f.rb` to execute it. A wrong number in the conversion formula is different: it can produce a wrong answer even when the syntax is valid. section 1.1.6

The installation also includes tools around the language: `ri` and `rdoc` for documentation, `rake` for tasks, `gem` and Bundler utilities for packages, and `rdbg` for debugging. These complement the core syntax and the facilities for libraries and extensions. section 1.0 section 1.4

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Builds from basic syntax and interpreter checks to objects and message calls; defers an in-depth treatment of classes.
Examples
Uses irb arithmetic, input/output snippets, conditional expressions, and a Celsius-to-Fahrenheit file with syntax and output problems.
What it assumes
Assumes some programming experience while introducing Ruby syntax and tools needed to begin experimenting.
What you can do after
Practice/perform Ruby experiments in irb, check a program with `ruby -cw`, and run it with `ruby`.
How it connects
Connects object-oriented programming and method calls with self, classes, libraries, and command-line tools.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.