Sunday, March 16, 2014

Learning Notes about Metaprogramming Ruby

1. Metaprogramming is writing code that manipulates language constructs at runtime.

2. Named Arguments allows you to set arguments by names rather than by position.

3. Argument array, the * operator collects multiple arguments in a single array.

def my_method(*args)
   args
end

my_method(1, '2', 'three') # => [1, "2", "three"]

4. Self Yield
When you pass a block to a method, you expect the method to call back to the block through yield.

5. Metaprogramming: design a Domain Specific Language (DSL) and then using that DSL to write your own program.

6. An object's instance variables live in the object itself, and an object's methods live in the object's class. (Objects of the same class share methods but don't share instance variables)

7. A class is just a souped-up module with 3 additional methods---new(), allocate() and supreclass()---that allows you to create objects or arrange classes into hierarchies.

8. Classes are nothing but objects, class names are nothing but constants.

9. Module is to be imported somewhere (or used as a Namespace) and class is to be instantiated or inherited.

10. load() to execute code and require() to import libraries.

11. When you call a method, actually you are sending a message to an object.

12. Symbol is immutable and some operations (such as comparison) run faster on symbol rather than on string. Symbol is used as the name of things, particularly, name of metaprogramming related things such as methods.

13. Black slate, a class that has fewer methods than the Object class itself since removing most inherited methods from your proxies right away.

14. Procs vs. Lambdas
1) In a lambda, return just returns from the lambda.
2) In the way check their arguments, lambda tends to be less tolerant than procs (and regular blocks).

15. Blocks(they aren't really "objects", but they are still callable): Evaluated in the scope in which they're defined.

Procs: Objects of class Proc. Like blocks, they are evaluated in the scope where they're defined.

Lambdas: Also objects of class Proc but subtly different from regular procs. They're closures like blocks and procs, and as such they're evaluated in the scope where they're defined.

Methods: Bound to an object, they are evaluated in that object's scope. They can also be unbound from their scope and rebound to the scope of another object.

16. Around Alias in three steps:
1) You alias a method.
2) You redefine it.
3) You call the old method from the new method.

17. When you learn that, when it comes right down to it, code is just text.

18. A master developer sits on top of mountain, meditating.
You are smart enough to learn, but are you smart enough to forget what you have learned? There's no such thing as metaprogramming. It's just programming all the way through.

19. Trade-off of metaprogramming
Complexity for beginner vs Complexity for experts

Internal Complexity vs External Complexity (By making insides of your code more complex, you make your library simpler for clients.

Complexity by Terseness vs Complexity by Duplication

Complexity for humans vs Complexity for tools

20. Class Extension Mixing turns methods in module into class methods, modules make code easier to understand and change.

21. Metaprogramming code can get complex but you can manage the complexity with unit testing which help you write code clean and error-free.

22. Ruby with Metaprogramming expects you manipulate the language constructs, tweak the object model, reopen classes, define methods dynamically, and manage scopes with blocks. (There's no such thing as metaprogramming. It's just programming all the way through.)