In Ruby's world, programmers enjoy all kinds of bizarre grammatical sugars, and also go through a variety of traps. And it's all about Ruby's powerful metaprogramming capabilities. Metaprogramming is like the magic of the Ruby World, and when it is white magic can help you to make the program very concise, beautiful, and when it is dark magic, you will be lost in some difficult to explain the bug.
Ruby Metaprogramming is a cheat book that tells you how to use and control the magic of Ruby Metaprogramming. The writing technique of the book is very commendable, the author concentrates all the knowledge points in the one-week work process, through a rookie and Daniel for the project encountered in the various issues of discussion, solve to introduce a variety of meta programming knowledge points. In addition, at the end of each knowledge point is also accompanied by an interesting quiz, so that the reader can follow the rookie ideas, feel himself in a step-by-step grasp of the idea of metaprogramming. This arrangement makes the book very interesting to read, and the theoretical knowledge in the book, combined with the actual practice in the project, makes it easier for readers to think about how to use this knowledge in their own projects.
I heard about the book from my colleague's mouth, and after reading the book, he said, "This book basically changes its code-writing habits." "As a programmer for more than 10 years of age. So praise a book, let me determination must read this book, after reading, this book does not bear this praise. Whether it's a beginner programmer or a programmer, you should read this book, and if you're a ruby programmer, this book can be considered one of the necessary readings. The book is divided into 2 parts. The first part, from the object model, methods, code blocks, class definitions, and so on one by one analysis of ruby design principles, and then through an example to tell you how to effectively use these design principles in practical applications, and the author is also very kind to remind everyone in the use of these skills when the precautions to prevent these magic into black magic. The second part is an analysis of the various metaprogramming techniques used in rails, and after reading it, it's helpful to understand the rails underlying implementation, and, of course, you can skip over the rails without loving readers.
Object model
Referring to objects, the first thing programmers think of is the concept of class, in the first chapter of this book, the author first on the Ruby world classes have a basic explanation:
Unlike static languages such as Java, class definitions can only execute statements that define variables and methods, and in Ruby, class-defined code is the same as other code, where any ruby statement can be executed.
Ruby is born with the ability to open an existing class and dynamically modify its content, but pay attention to the problem of monkey patches.
The instance variables of the class are stored in the object. An instance variable is not related to the class of the object, and when an instance variable is assigned to an object, the instance variable is generated, and the instance variable is like a hashmap on the object, and each object can have its own different hashmap.
The definition of a method is in the class of the object itself, because "objects that share the same class must also share the same method." However, it cannot be said that class has a method called "methods," because the method cannot be invoked using "Class.method", and that class has an instance method "methods", which means that the instance object of the class must be created and invoked by the instance object.
You can also define a class method in Ruby, or a class macro that, when you define a method, adds "self." before the method name. or "class name." Prefix, you can then use the method in the class like a keyword, and rely on class macros to implement many very concise DSLs.
The class itself is also an object, and rules on all instance objects can also be applied to the class object itself.
The inheritance system of the class: