0% found this document useful (0 votes)
1 views

Metaprogramming __

The document explains the object model in Ruby, highlighting that classes are treated like any other object and can be modified dynamically. It discusses the relationship between classes, modules, and instance variables, emphasizing that instance variables are unique to each object while methods are shared among instances of the same class. Additionally, it covers method lookup, the role of 'self', and the concept of private methods in Ruby.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Metaprogramming __

The document explains the object model in Ruby, highlighting that classes are treated like any other object and can be modified dynamically. It discusses the relationship between classes, modules, and instance variables, emphasizing that instance variables are unique to each object while methods are shared among instances of the same class. Additionally, it covers method lookup, the role of 'self', and the concept of private methods in Ruby.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

The Object Model

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.

●​ An object is composed of a bunch of instance variables and a link to a class.


●​ The methods of an object live in the object’s class. (From the point of view of the class,
they’re called instance methods.)
●​ The class itself is just an object of class Class. The name of the class is just a constant.
●​ Class is a subclass of Module. A module is basically a package of methods. In addition to that, a
class can also be instantiated (with new) or arranged in a hierarchy (through its superclass).
●​ Constants are arranged in a tree similar to a file system, where the names of modules and
classes play the part of directories and regular constants play the part of files.
●​ Each class has an ancestors chain, beginning with the class itself and going up to BasicObject.
●​ When you call a method, Ruby goes right into the class of the receiver and then up the
ancestors chain, until it either finds the method or reaches the end of the chain.
●​ When you include a module in a class, the module is inserted in the ancestors chain right
above the class itself. When you prepend the module, it is inserted in the ancestors chain right
below the class.
●​ When you call a method, the receiver takes the role of self.
●​ When you’re defining a module (or a class), the module takes the role of​
self.
●​ Instance variables are always assumed to be instance variables of self.
●​ Any method called without an explicit receiver is assumed to be a method of self.
●​ Refinements are like pieces of code patched right over a class, and they override normal
method lookup. On the other hand, a Refinement works in a limited area of the program: the
lines of code between the call to using and the end of the file, or the end of the module
definition.

You might also like