Saved by Harold T. Harper
The Well-Grounded Rubyist
As in a book with multiple first-person narrators, the I role can get passed around. There’s always one self, but what object it is will vary. The rules of scope govern the visibility of variables (and other elements, but largely variables).
Joe Leo • The Well-Grounded Rubyist
You can nest a class definition inside a module definition like this: module Tools class Hammer end end To create an instance of the Hammer class defined inside the Tools module, you use the double-colon constant lookup token (::) to point the way to the name of the class: h = Tools::Hammer.new Nested module/class chains like Tools::Hammer are some
... See moreJoe Leo • The Well-Grounded Rubyist
This chapter will introduce you to a Ruby construct that’s closely related to classes: modules. As their name suggests, modules encourage modular design: program design that breaks large components into smaller ones and lets you mix and match object behaviors.
Joe Leo • The Well-Grounded Rubyist
Our price= method can be described as an attribute writer method.
Joe Leo • The Well-Grounded Rubyist
Any time you see repetition on that scale, you should try to trim it—not by reducing what your program does, but by finding a way to express the same thing more concisely. In pursuit of this conciseness, Ruby is one step ahead: it provides a built-in shortcut that automatically creates a method that reads and returns the value of the instance varia
... See moreJoe Leo • The Well-Grounded Rubyist
The fact that Ruby has classes and modules—along with the fact that from an object’s perspective, all that matters is whether a given method exists, not what class or module the method’s definition is in—means you have a lot of choice when it comes to your programs’ design and structure.
Joe Leo • The Well-Grounded Rubyist
Each ticket has its own state (saved information), thanks to what our initialize method does; and each ticket lets us query it for the venue and date, thanks to the two methods with those names.
Joe Leo • The Well-Grounded Rubyist
When you use the dot notation on a class, you send a message to the class. Classes can respond to messages, just like objects; in fact, as you’ll have reason to be aware of in any number of situations, classes are objects. The new method is a constructor: a method whose purpose is to manufacture and return to you a new instance of the class, a newl
... See moreJoe Leo • The Well-Grounded Rubyist
The main difference between inheriting from a class and mixing in a module is that you can mix in more than one module. No class can inherit from more than one class. In cases where you want numerous extra behaviors for a class’s instances—and you don’t want to stash them all in the class’s superclass and its ancestral classes—you can use modules t
... See moreJoe Leo • The Well-Grounded Rubyist
Ruby gives you a number of ways to control program flow on a conditional basis. The most important ones fall into two categories: if and related keywords case statements