Metaprogramming __
Metaprogramming __
In Ruby, there is no real distinction between code that defines a class and code of any other
kind. You can put any code you want in a class definition
Ruby executed the code within the class just as it would execute any other code. Does that mean you
defined three classes with the same name? The answer is no
In a sense, the class keyword in Ruby is more like a scope operator than a class declaration. It has an
important practical consequence: you can always reopen existing classes— even standard library
classes such as String or Array—and modify them on the fly. You can call this technique Open Class.
in Ruby there is no connection be- tween an object’s class and its instance variables. Instance
variables just spring into existence when you assign them a value, so you can have objects of the
same class that carry different instance variables.
an object’s instance variables live in the object itself, and an object’s methods live in the object’s
class. That’s why objects of the same class share methods but don’t share instance variables.
a Class in Ruby is quite literally the class itself, and you can manipulate it like you would manipulate
any other object. The methods of an object are also the instance methods of its class. In turn, this
means that the methods of a class are the instance methods of Class:
The superclass of Class is Module—which is to say, every class is also a module. To be precise, a class
is a module with three additional instance methods (new, allocate, and superclass) that allow you to create
objects or arrange classes into hierarchies. Usually, you pick a module when you mean it to be
included somewhere, and you pick a class when you mean it to be instantiated or inherited. What’s a
class? It’s an object (an instance of Class), plus a list of instance methods and a link to a superclass.
Class is a subclass of Module, so a class is also a module.
Any reference that begins with an uppercase letter, including the names of classes and modules, is a
constant. If you can change the value of a constant, how is a constant different from a variable? The
one important difference has to do with their scope. The scope of constants follows its own special
rules, as you can see in the example that follows.
The require method is quite similar to load, but it’s meant for a different purpose. You use load to execute code, and you use
require to import libraries. require tries only once to load each file, while load executes the file again every time you call it. require
tries only once to load each file, while load executes the file again every time you call it.
The process of method lookup in a single sentence: to find a method, Ruby goes in the
receiver’s class, and from there it climbs the ancestors chain until it finds the method.
if that module is already in the chain, Ruby silently ignores the second inclusion. As a result, a module
can appear only once in the same chain of ancestors.
Every line of Ruby code is executed inside an object—the so-called current object. The current object
is also known as self, because you can access it with the self keyword. Only one object can take the
role of self at a given time, but no object holds that role for a long time. In particular, when you call a
method, the receiver becomes self. From that moment on, all instance variables are instance vari-
ables of self, and all methods called without an explicit receiver are called on self. As soon as your
code explicitly calls a method on some other object, that other object becomes self.
Private methods are governed by a single simple rule: you cannot call a private method with an explicit receiver. In other words,
every time you call a private method, it must be on the implicit receiver—self. first, you need an explicit receiver to call a method
on an object that is not yourself, and second, private methods can be called only with an implicit receiver.
In a class or module definition (and outside of any method), the role of self is taken by the class or
module itself.