From: Frederick Cheung Date: 2008-08-20T00:12:21+09:00 Subject: Re: Array API On 19 Aug 2008, at 15:09, Robert Klemme wrote: >>> >>> How it is possible that we are able to use enumerable sort_by >>> method for >>> an array of object? >>> >> Because that's what including a module does: it adds the methods >> defined in >> the module to the class. > > This is only true in a very general sense, i.e. if you want to express > with this that you can invoke methods defined in a module on an > instance of a class that directly or indirectly (!) inherits the > module. > > irb(main):002:0> Array.instance_method :sort_by > => # > irb(main):003:0> Array.instance_method :size > => # > > Note the difference between #sort_by (defined in Enumerable) and #size > (defined in Array). > > Rather the module is inserted into the lookup path (see output of > #ancestors above). Otherwise methods from different modules could not > invoke each other via super but instead one would overwrite the other: > Of course. it would also mean that adding methods to a module after it had been included wouldn't work. Fred