
Saved by Harold T. Harper
Learn to Program
Saved by Harold T. Harper
main is a special term that the default self object uses to refer to itself. You can’t refer to it as main; Ruby will interpret your use of main as a regular variable or method name. If you want to grab main for any reason, you need to assign it to a variable at the top level: m = self
Objects are represented either by literal constructors—like quotation marks for strings—or by variables to which they’ve been bound. Message sending is achieved via the special dot operator: the message to the right of the dot is sent to the object to the left of the dot. (There are other, more specialized ways to send messages to objects, but the
... See moreOne peculiarity of Ruby constants is that they aren’t constant. You can change them, in two senses of the word change—and therein lies an instructive lesson.
From our earliest examples onward, we’ve been making bareword-style calls to puts and print, like this one: puts "Hello" puts and print are built-in private instance methods of Kernel—not, like the ones you write, of Object, but of Kernel. The upshot is similar, though (because Object mixes in Kernel): you can call such methods at any tim
... See moreThe names of the instance variables, methods, and arguments to initialize don’t have to match. You could use @v instead of @venue, for example, to store the value passed in the argument venue. You could call the second method event_date and use @date inside it. Still, it’s usually good practice to match the names to make it clear what goes with wha
... See moreThe bread-and-butter way to maintain state in an object is the instance variable. Class variables come in handy because they break down the dam between a class object and instances of that class. But by so doing, and especially because of their hierarchy-based scope, they take on a kind of quasi-global quality: a class variable isn’t global, but it
... See more