Array does not have sort_by ; Enumerable has it. How it works?
Array
sort_by
Enumerable
%w[aa aaaa aaa].sort_by{|item| item.length} #=> ['aa','aaa','aaaa']
Shouldn't that throw an error, for example undefined method sort_by ? What is the relationship between Array and Enumerable ?
undefined method sort_by
The class Array includes a module Enumerable . You can see this in the documentation on the left under Included Modules.
Enumerable mixes up with class Array : all methods defined by Enumerable are available for Ruby arrays.
Array.ancestors # => [Array, Enumerable, Object, Kernel, BasicObject]
This is called inheritance. If the method is not found in the object class, Ruby lifts up the ancestor chain until it finds one.
Array inherits from Enumerable (more precisely, it mixes in Enumerable , which is a form of inheritance).