Note: Ruby-Enumerable

Michael Hwang
3 min readJul 12, 2019

In the beginning, here is some note about class and module in Ruby. Class generally can view as some data exist, or say it is an instance, while module more like a function and can be not only used by a single class.

A quick example is, a dog (class) will bark and a cat (class)will meow (These action as there class method), and they both will eat, sleep (Method from the module, maybe is called as daily life). It is the same to call both types of function but should be careful about where the method from so won't make an error when change type.

In this chapter, we will focus on the Enumerable Module, which is included in Array and Enumerator classes.

About Module

Enumerable Module provides several traversal and searching methods, and also the ability to sorting. The applied class require each function for this module, which yields successive members of collections.

# The word pattern stands for Range and Regexp object.
# === operator is used to compare whether an element follows the pattern

Methods

Traversal

all?{}: Whether all element suits the criteria from the block. (If No block is given will check all element is nil/false or not)

any?{}: Similar to all?, but checking if any element matches.

each_cons(n){}: Iterating for each array of consecutive n elements.
each_slice(n){}:Iterating for each slice of n elements
each_with_index{|obj, i|}: Iterating with index
each_with_object{|obj, other_obj|}: Iterating with arbitrary object, and return it in the end.

inject/reduce(initial){|memo, obj| block}: Combined all element by applied a binary operation. Can be specified by block or symbol (operator or method). Each element will pass to the block or method, and accumulate the value through memo, and return the last value of memo.

Selecting

grep(pattern){}: Selected the element matching the pattern and passed them to block if block exists.
grep_v(pattern): Opposite version of grep.

max_by(n){|obj|}: Get n elements given maximum value from the block in descending order.
min_by(n){|obj|}: Get n elements given minimum value from the block in ascending order.
minmax_by{}: Get the elements given minimum and maximum value. [min, max]
# All of the min_by, max_by, minmax_by methods have a without “by” version, which requires <=> input, and can decide the order.

slice_after{|elt| bool}
slice_before{|elt| bool}
slice_when{|elt_before, elt_after| bool}
#Slice the enum depend on true/false value according to the boolean test from the block.
# Cut the enum when true happened, if is slice_after then cut after that, if is slice_before then cut before that.
# Slice_when will iterate with two elements consecutively, and separate them when true happen. That is, the cut point is between them.

chunk{}: Chunking the element based on the return value from the block. Consecutive element returning the same value are chunked together.
# chunk_while: Do the same thing as slice_when but cut when false happen.

Sorting

group_by{}: Group the element by the criteria in the block, and return a Hash

partition{}: Return two arrays, under the criteria from the block, the first includes the element is“True” and the second is “False”.

Other

lazy: It won't iterate immediately unless you call some part of it. Some methods are unusable unless using “force” to force it. (Generall the method will run through all the elements or the run from the last)
# This is useful when iterating on enormous or infinite data.

Already learn from Array:

map/collect
drop
take
count
select/filter
reject
find, find_all, find_index
include?/member?
first, last
cycle
uniq

--

--