What classes are Enumerable in Ruby?

Enumerable is a powerful mixin that provides some really slick operations on collections. I started looking at it since I think it is important to teach map and inject, but ended up teaching about most of its methods, since they are so cool. I found a great series of exercises and added to it to create a worksheet for the Rails class I’m teaching.

I wondered how to find whether a class is Enumerable and which Ruby classes were Enumerable. I was hoping to find some kind of picture of class inheritance for Ruby, but @techiferous pointed out Ruby’s ObjectSpace and @MikeG1 provided a the exact snippet of Ruby code that showed me exactly what I wanted:

ObjectSpace.each_object(Class) {|cl| puts cl if cl < Enumerable}

Post a Comment

Your email is never shared. Required fields are marked *

*
*