Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the relationship between Array and Enumerable?

Tags:

arrays

ruby

Array doesn't have sort_by; Enumerable has it. How does this work?

%w[aa aaaa aaa].sort_by{|item| item.length} #=> ['aa','aaa','aaaa']

Doesn't this have to throw an error like undefined method sort_by? What is the relationship between Array and Enumerable?

like image 437
Canna Avatar asked Sep 07 '25 18:09

Canna


1 Answers

The Array class includes the Enumerable module. You can see that in the documentation on the left under "Included Modules".

like image 172
Logan Serman Avatar answered Sep 09 '25 08:09

Logan Serman