Metaprogramming Flashcards
What is Metaprogramming?
Metaprogramming is writing code that manipulates language constructs at runtime.
What is Open Class?
Open Class is when you can extend or overwrite an existing class ( String in ruby for example )
What are the main disadvantages of metaprogramming?
- Overwriting core methods
What is Monkeypatching?
Monkey Patch (MP) is any dynamic modification to a class at runtime.
What is Dynamic Dispatch?
You can wait literally until the very last moment to decide which method to call, while the code is running. (send method)
What is Dynamic Method?
This technique of defining a method at runtime is called a Dynamic Method. ( define_method )
What is Ghost Method?
This is a method which are actually don’t exist. ( methiod missing)
Why do we need Proc and Lymbda?
Because in Ruby block is not object. And if we want to store block somewhere we need to use a variable.
Why do we need class_eval and instance_eval?
Class_eval allows to add new method to the class Intance_eval allows to add new methods to the instance of class.
class Hello end
Hello.class_eval do def hello p 'Hello from class Eval' end end
hello = Hello.new hello.hello # 'Hello from class Eval'
hello.instance_eval do def hello2 p 'Hello from instance eval' end end hello.hello2 # 'Hello from instance eval' Hello.new.hello2 #error