Chapter 5. The default object (self), scope, and visibility

 

In this chapter

  • The role of the current or default object, self
  • Scoping rules for local, global, and class variables
  • Constant lookup and visibility
  • Method access rules

 

In describing and discussing computer programs, we often use spatial and, sometimes, human metaphors. We talk about being “in” a class definition or returning “from” a method call. We address objects in the second person, as in obj.respond_to?("x") (that is, “Hey obj, do you respond to ‘x’?”). As a program runs, the question of which objects are being addressed, and where in the imaginary space of the program they stand, constantly shifts.

And the shifts aren’t just metaphorical. The meanings of identifiers shift ...

Get The Well-Grounded Rubyist now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.