- Author: Damon Yuan
- Date: 2017-09-14
The new keyword in front of any function turns the function call into constructor call and below things occurred when new keyword put in front of function
- A brand new empty object gets created
- new empty object gets linked to prototype property of that function
- same new empty object gets bound as this keyword for execution context of that function call
- if that function does not return anything then it implicit returns this object.
Note: the foo object in ruby diagram also has an corresponding singleton class. And the
extend key word is a short-cut to open the singleton class.
moddule m // here is the module // do whatever you want end foo.exend m
foo.instance_eval do // here the singleton class of object foo is open // do whatever you want end
By the way, Metaprogramming Ruby 2 is really great a book for rubyists.