Ruby Enumerable Module (Part 2)

Giulia Elizabeth
3 min readMay 19, 2021

https://www.pinterest.com/pin/801781539907236988/

In sequel to Ruby Enumerable Article (Part 1), in this article we will go over some additional ruby methods. These methods include to_a, to_h, uniq, max, max_by, min, min_by, minmax, minmax_by, sort, sort_by, reject, partition, sum, slice_after, slice_before, slice_when, inject, reduce, lazy, and zip.

  • to_a: returns the given enumerable in array form
(1...6).to_a #=>  [1, 2, 3, 4, 5]{'a': 1, 'b':2}.to_a #=> [[:a, 1], [:b, 2]]
  • to_h: returns the enumerable as a hash, where the key and value pairs are specified in the code block
(1...6).to_h{|x| [x, 'number']}#=>{1=>"number", 2=>"number", 3=>"number", 4=>"number", 5=>"number"}
  • uniq: returns an array with all duplicate values removed
[1,2,3,5,5].uniq #=>[1, 2, 3, 5]
  • max: return the max value, if n is given will return n values in array form. If block is given will return item with max value for given “spaceship” comparison.
[1,2,3,5,5].max(2) #=> [5,5]
['bob','sally','tom','joseph','z'].max{|a, b| a.length <=> b.length}
#=>"joseph"
  • max_by: return the max value based on the given block
['bob','sally','tom','joseph','z'].max_by { |x| x.length }
#=>"joseph"
['bob','sally','tom','joseph','z'].max_by(2) { |x| x.length }
#=>["joseph", "sally"]
  • min: return the min value, if n is given will return n values in array form. If block is given will return item with min value for given block.
[1,2,3,5,5].min(2) #=> [1, 2]
  • min_by: return the min value based on the given block
['bob','sally','tom','joseph','z'].max_by(2) { |x| x.length }
#=>["z", "tom"]
  • minmax: return the min and max value. If block is given will return item with min and max value for given “spaceship” comparison.
(1...6).minmax #=>[1, 5]['bob','sally','tom','joseph','z'].minmax{ |a, b| a.length <=> b.length }
#=>["z", "joseph"]
  • minmax_by: return the min and max value representing the current block
['bob','sally','tom','joseph','z'].minmax_by { |x| x.length }
#=>["z", "joseph"]
  • sort returns an array in sorted in ascending order, or that follows the given “spaceship” comparison
[5,6,8,1,3,2].sort
#=>[1, 2, 3, 5, 6, 8]
['b','a','d','z','g'].sort
#=>["a", "b", "d", "g", "z"]
{'z':1,'a':2,'g':3}.sort
#=>[[:a, 2], [:g, 3], [:z, 1]]
(1..10).sort { |a, b| b <=> a }
#=>[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
  • sort_by: returns a sorted array that follows the given block
['bob','sally','tom','joseph','z'].sort_by { |word| word.length }
#=> ["z", "bob", "tom", "sally", "joseph"]
  • reject: return all elements that return true
(1...6).reject(&:even?)
#=>[1, 3, 5]
  • partition: returns two arrays, first with elements that return true, second withe elements that return false
(1...6).partition(&:even?)
#=>[[2, 4], [1, 3, 5]]
  • sum: returns the sum of the given elements. If block is provided, block is performed on each element, before the addition.
(1...6).sum #=>15[1,2,3].sum{|x| x*2} #=> 12{1=>10,2=>25,3=>6}.sum{|k, v| v } #=> 41{1=>10,2=>25,3=>6}.sum{|k, v| k } #=> 6
  • slice_after: creates an enumerable for each chunk of elements, each chunk is ended by the element that returns true to the condition
(1...10).slice_after(&:even?).to_a
#=> [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
(1...10).slice_after(5).to_a
#=> [[1, 2, 3, 4, 5], [6, 7, 8, 9]]
  • slice_before: creates an enumerable for each chunk of elements, each chunk is started by the element that returns true to the condition
(1...10).slice_before(5).to_a
#=>[[1, 2, 3, 4], [5, 6, 7, 8, 9]]
(1...10).slice_before(&:even?).to_a
#=> [[1], [2, 3], [4, 5], [6, 7], [8, 9]]
  • slice_when: creates an enumerable for each chunk of elements, each chunk is started by the element that returns true to the condition
(1...10).slice_when{|i,j| i.even?}.to_a
#=>[[1, 2], [3, 4], [5, 6], [7, 8], [9]]
(1...10).slice_when{|i,j| i<j}.to_a
#=> [[1], [2], [3], [4], [5], [6], [7], [8], [9]]
  • reduce: combines elements based on a given symbol or block of code (same as inject)
(1..4).reduce(:*) #=>  24(1..4).reduce(2,:*) #=> 48(1..4).reduce(2) { |product, n| product * n } #=> 48
  • lazy: postpones enumeration to an as need bases. The below examples show how this enumerable may be used. In the first example the operation will never execute as the first iteration will be going through an infinite amount of numbers. In the second the lazy operator is used, and the code is executed as it is only looking for the first 5 elements.
Prime.reject { |i| i.even? }.first(5) #=>.....Prime.lazy.reject { |i| i.even? }.first(5) #=> [3, 5, 7, 11, 13]
  • zip: takes the first block of elements and merges each item with the corresponding element at that index.
['a','b','c'].zip([1,2,3])
#=> [["a", 1], ["b", 2], ["c", 3]]
['a','b','c'].zip([1,2,3],['*','$','#'])
#=> [["a", 1, "*"], ["b", 2, "$"], ["c", 3, "#"]]
[1,2,3,4].zip(['a','b','c'],['*','$','#'])
#=>[[1, "a", "*"], [2, "b", "$"], [3, "c", "#"], [4, nil, nil]]

https://ruby-doc.org/core-3.0.1/Enumerable.html#

https://giuliacajati.medium.com/ruby-enumerable-module-5fab6021f40b

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response