2014年10月15日 星期三

[ 文章收集 ] How Ruby method dispatch works

Source From Here 
Preface 
I was asking around to see if anyone knew a good, short explanation of Ruby’s object and method dispatch system the other day, and the response from several people was, “no, you should write one.” So, here we are. I’m going to explain how Ruby’s object system works, including method lookup, inheritance, super calls, classes, mixins, and singleton methods. My understanding comes not from reading the MRI source but from reimplementing this system, once in JavaScript and once in Ruby. If you want to read a minimal but almost correct implementation that Ruby gist is not a bad place to start. 

Because I’ve not actually read the source, this will explain what happens logically but it might not be what actually happens inside of Ruby. It’s just a model you can use to understand things. 

How Ruby method dispatch works 
Right, let’s start at the start. You can build almost all of Ruby’s object system out of Module. Think of a module as a bag of methods. For example, module A contains methods foo and bar. 
 

When you write def foo ... end inside a Ruby module, you are adding that method to the module, that’s all. Now, a module can have any number of ‘parents’: 
  1. require "A"  
  2.   
  3. module B  
  4.     include A  
  5.     def hello  
  6.         puts "B's hello"  
  7.     end  
  8.     def bye  
  9.         puts "B's bye"  
  10.     end  
  11. end  
all you are doing is adding A as a ‘parent’ of B. No methods are copied, we just create a pointer from B to A
 

Now, a module can have many parents, and they form a tree. Take these modules: 
  1. module C  
  2.     include B  
  3.     def start  
  4.         puts "C's start"  
  5.     end  
  6.     def stop  
  7.         puts "C's stop"  
  8.     end  
  9. end  
  10.   
  11. module D  
  12.     include A  
  13.     include C  
  14. end  
These form a tree like this, following their include relationships: 
 

An important concept that affects how methods are dispatched is a module’s ‘ancestry’. You can ask a module for its ancestors and it will give you an array of modules: 
>> require "D"
=> true
>> D.ancestors
=> [D, C, B, A]

The important thing about this list is that it’s flat, rather than being a tree. It determines the order that we search modules in to find a method. To build this list, we start at D and run a depth-first right-to-left search of its tree. This is why the order of include calls is important: a module’s parents are ordered and this determines the order they are searched in. 

When we want to dispatch a method, we look at each one of a module’s ancestors in turn, and stop at the first module that contains a method with the name we want. If none of the modules contain this method, we perform the search again but this time looking for the method called method_missing. If none of the modules contain that method, we throw a NoMethodError

We can use Ruby’s reflection capabilities through instance_method to find out which method will be used when we invoke certain names: 
>> D.instance_method(:foo)
=> #A
)#foo>
>> D.instance_method(:hello)
=> #B)#hello>
>> D.instance_method(:start)
=> #C)#start>
An UnboundMethod is just an object representing a method from a module, before it’s been bound to an object. When you see D(A)#foo, it means D has inherited the #foomethod from A. If you dispatch #foo to an object that includes D, you’ll get the method defined in A

Speaking of objects, why haven’t we made any yet? What good is a bag of methods will no objects to invoke them on? Well, that’s where Class comes in. In Ruby, Class is a subclass of Module, which sounds weird but just remember they’re data structures that hold methods. A Class is like a Module, in that it’s a thing that stores methods and can include other modules, but it also has some additional capabilities, the first of which is that it can create objects. 
  1. class K  
  2.   include D  
  3. end  
  4.   
  5. k = K.new  
Again, we can use reflection through method to see where each of the object’s methods come from: 
>> k.method(:start)
=> #C
)#start>
This shows that when we invoke k.start, we’ll get the #start method from module C. You’ll notice that while calling instance_method on a module gets us anUnboundMethod, calling method on an Object gets us a Method. The difference is that a Method is bound to an object; it’s a callable that, when you invoke #call on it, will do the same thing as calling k.startUnboundMethod cannot be called directly since they have no object to be invoked on. 

So it looks like we dispatch method calls by finding the class the object belongs to, then looking through that class’s ancestors until we find a matching method. That’s almost true, but Ruby has another trick up its sleeve: singleton methods. You can add new methods to any object, and only that object, without adding them to a class. See: 
>> def k.mart; end
=> nil
>> k.method(:mart)
=> #.mart>

We can add them to modules too, since modules are just another kind of object
>> def B.roll; end
=> nil
>> B.method(:roll)
=> #

When a Method‘s name has a dot (.) instead of a hash (#) in it, it means the method exists only on that object instead of being contained in a module. But we said earlier that modules are the thing Ruby uses to store methods; plain old objects don’t have this power. So where are singleton methods stored? 

Every object in Ruby (and remember, modules and classes are objects too) has what’s called a metaclass, also known as a singleton class, eigenclass or virtual class. The job of this class is simply to store the object’s singleton methods; by default it contains no methods and has the object’s class as its only parent. So for our object k, its full ancestor tree looks like this: 
 

We can ask Ruby for an object’s metaclass, and reflect on it just like any other. Here we see the metaclass is an anonymous Class attached to the object k(Object.singleton_class), and it has an instance method #mart that doesn’t exist in the K class. 
>> k.singleton_class
=> #>
>> k.singleton_class.instance_method(:mart)
=> #>#mart>
>> k.instance_method(:mart)
NoMethodError: undefined method `instance_method' for #

One gotcha to look out for is that metaclasses don’t appear in their own #ancestors lists, but you should think of them being in their for the purposes of finding methods. When we invoke methods on k, it asks its metaclass to find the method, and this uses the metaclass’s ancestry to locate the required method. Singleton methods live in the metaclass itself, so they are preferred over methods inherited from the object’s class or any of its ancestors. 

Now we come to the second special property of classes, beyond their ability to create objects. Classes have a special form of inheritance called ‘subclassing’. Every class has one and only one superclass, the default being Object. In terms of method lookup, you can think of a superclass as just being the class’s first parent module
 

So Foo.ancestors gives us [Foo, Extras, Bar] in both cases, and this determines method lookup order as usual. (Actually it gives us [Foo, Extras, Bar, Object, Kernel, BasicObject] but we’ll get to those letter modules in a minute.) Note that Ruby violates the Liskov substitution principle by not allowing classes to be given to include; only modules can be used this way, not their subtypes. The above snippet simply expresses what subclassing means for method lookup, and the code on the right will not run if Bar is a Class. 

If subclassing is the same as including, why do we need it at all? Well, it does one extra thing: classes inherit their superclass’s class methods, but not those of included modules. 
 

We can model this in terms of parent relationships by saying that the subclass’s metaclass has the superclass’s metaclass as a parent: 
 

And indeed if we reflect on Foo we see that its #bar method originates from Bar‘s metaclass. 
>> Foo.method(:bar)
=> #
>> Foo.singleton_class.instance_method(:bar)
=> ##bar>

We’ve seen how inheritance and method lookup in Ruby can be modelled as a tree of modules, with include and subclassing creating various parent relationships. This describes single and multiple inheritance of instance and singleton methods pretty well. Now let’s look at a few things that piggy-back on this model. 

The first is the Object#extend method. Calling object.extend(M) makes the methods in module M available on objectIt doesn’t copy the methods, it just adds M as a parent of the object’s metaclass. If object has class Thing, we get this relationship: 
 

So extending an object with a module is just the same thing as including that module in the object’s metaclass. (Actually there are some differences but they’re not relevant to the present discussion.) Given this tree, we see that when we invoke methods on object, the lookup process will prefer methods contained in M to those defined in Thing, and will prefer methods defined directly in the object’s metaclass over both of them. 

This context is important: we cannot say methods in M take precedence over Thing in general, only when we’re talking about method calls to objectThe method receiver’s ancestry is what’s important, and this shows up when we investigate how super works. Take this set of modules: 
  1. module X  
  2.   def call ; [:x] ; end  
  3. end  
  4.   
  5. module Y  
  6.   def call ; super + [:y] ; end  
  7. end  
  8.   
  9. class Test  
  10.   include X  
  11.   include Y  
  12. end  
The ancestry of Test goes [Test, Y, X], so clearly if we call Test.new.call we will invoke the #call method from Y. But what happens when Y calls super? Y has no ancestors of its own, so there’s nowhere to dispatch the method to, right? 
 

To dispatch the method, we invoke the first method in this list. If that method calls super, we jump to the second, and so on until we run out of methods to invoke. If Testdidn’t include module X, there would be no implementations of #call after the one from Y so that call to super would fail. 

Sure enough, in our case Test.new.call returns [:x, :y]

We’re almost done, but I promised I’d explain what ObjectKernel and BasicObject are. BasicObject is the root class of the whole system; it’s a Class with no superclass.Object inherits from BasicObject, and is the default superclass of all user-defined classes. The difference between the two is that BasicObject has almost no methods defined in it, while Object has loads: core Ruby methods like #==, #__send__, #dup, #inspect, #instance_eval, #is_a?, #method, #respond_to?, and #to_s. Well, actually it doesn’t have all those methods itself, it gets them from KernelKernel is just the module with all Ruby’s core object methods in it. So when we map out Ruby’s core object system we get the following: 
 

This shows the core modules and classes in Ruby: BasicObjectKernelObjectModule and Class, their metaclasses, and how they are all related. Yes,BasicObject.singleton_class.superclass is Class. Ruby does some voodoo internally to make this circular relationship work. Anyway, if you want to understand Ruby method dispatch, just remember: 
* A module is a bag of methods
* A module can have many parents
* A class is a module that can make new objects
* Every object has a metaclass that has the object’s class as its parent
* Subclassing means linking two classes and their metaclasses
* Methods are found via a depth-first right-to-left search of the receiver’s metaclass’s ancestry

Supplement 
Stackoverflow - Get the name of the currently executing method in Ruby 
Blog - Ruby’s define_method, method_missing, and instance_eval

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...