Ruby Enumerable Mixin Explained

Modules that you include into classes are also called Mixins. Once more, classes are defined so that objects can be instantiated off of them and modules are lacking this capability.

There are couple of important considerations when using modules from within classes. You can access all of the module methods after including it into class. However, if you include more than one module into class and these modules have method with the same name, only one method will be called from within the class. This method belongs to last included module.

One the useful Mixins in Rube is Enumerable module. You can gain over 22 useful methods by including this module into your class. Enumerable is the most common mixin in Ruby language as well. It helps you set up any data structure with capability to iterate over it. In fact, a lot of standard classes such as Dir, Hash, and String include Enumerable mixin in its implementations.

All of the methods available to you after inclusion of enumerable mixin are:

 

chunk

collect_concat

count

cycle

drop

drop_while

each_cons

each_entry

each_slice

each_with_object

find_index

first

flat_map

group_by

lazy

max_by

min_by

minmax

minmax_by

none?

one?

reduce

reverse_each

slice_before

take

take_while

Featured pages

Ruby

Set of Ruby Object Oriented Programming Language tutorials that cover such topics as Ruby strings, …

Rails

Rails Framework tutorial teaches you how to utilize de facto framework of choice for Ruby developme…

Ruby Duck Typing

“If an object quacks like a duck just go ahead and treat it as a duck” – this fun…

Regular Expressions

Ruby uses the =~ operator to check any given string against regular expression. For example, a stri…

Credit Card Number

Every last digit of any credit card is a check sum digit that is determined by all digits in front …

Ruby Arrays

Ruby Programming Language has built in support for Arrays. Arrays help you define some of the compl…

Ruby Hashes

Hashes are very similar to arrays in Ruby and hashes interface is similar to Ruby array interface. …

Ruby Code Block

Ruby is very unique language when it comes to code blocks. You can simply pass a code block to a me…