Enumerable Methods
This is intended to be a brief list of examples of Ruby enumerables applied to various objects. This is by no means exhaustive or even instructional, but is only intended to serve as a reminder of the syntax for various operations
# The module Enumerable provides the following methods:
Enumerable.instance_methods(false).sort
=> [:all?, :any?, :chunk, :collect, :collect_concat, :count, :cycle, :detect, :drop, :drop_while,
:each_cons, :each_entry, :each_slice, :each_with_index, :each_with_object, :entries, :find, :find_all,
:find_index, :first, :flat_map, :grep, :group_by, :include?, :inject, :lazy, :map, :max, :max_by,
:member?, :min, :min_by, :minmax, :minmax_by, :none?, :one?, :partition, :reduce, :reject, :reverse_each,
:select, :slice_after, :slice_before, :slice_when, :sort, :sort_by, :take, :take_while,
:to_a, :to_h, :zip]
Examples
Methods for Iteration
The methods :each, :each_cons :each_slice, :each_with_index, :each_with_object do something to some or all elements of your object. Hashes also come with iterator methods :each_key, :each_pair, :each_value
metal = {'Mayhem' => "Norway", '1349' => "Norway", 'Marduk' => "Sweden", 'Portal' => "Australia",
'Yob' => "USA", 'Cultes des Ghoules' => "Poland" }
metal.each_entry {|key, value| puts value + ':' + ' ' + key}
# Outputs
Norway: Mayhem
Norway: 1349
Sweden: Marduk
Australia: Portal
USA: Yob
Poland: Cultes des Ghoules
[1,3,666,5,7,-99,19,23].each_cons(2) {|pair| print pair}
# Outputs consecutive sub-arrays of size 2
[1, 3][3, 666][666, 5][5, 7][7, -99][-99, 19][19, 23]
black_metal = Hash.new
%w(Mayhem, 1349, Marduk, Darkthrone).each_with_index{ |item, index| black_metal[item] = index}
# Outputs
{"Mayhem,"=>0, "1349,"=>1, "Marduk,"=>2, "Darkthrone"=>3}
[0,1,1,2,3,5,8,13,21,34,55,89,144].each_slice(4) { |a| p a }
# Outputs
[0, 1, 1, 2]
[3, 5, 8, 13]
[21, 34, 55, 89]
[144]
Methods for Querying
The methods :all?, :any?, :include?, :member?, :none?, :one? return true or false depending on the outcome of the query. Hashes also come with query methods :has_key?, :has_value?
%w(Mayhem, 1349, Marduk, Darkthrone).all?{|band| band.include? "throne"}
=> false
%w(Mayhem, 1349, Marduk, Darkthrone).any?{|band| band.include? "throne"}
=> true
%w(Mayhem, 1349, Marduk, Darkthrone).none?{|band| band.include? "throne"}
=> false
%w(Mayhem, 1349, Marduk, Darkthrone).one?{|band| band.include? "throne"}
=> true
Methods for Sorting
The methods :sort, :sort_by, return an array in sorted order.
metal = {'Mayhem' => "Norway", '1349' => "Norway", 'Marduk' => "Sweden", 'Portal' => "Australia",
'Yob' => "USA", 'Cultes des Ghoules' => "Poland" }
metal.sort # Returns an array of key-value pairs sorted by key
=> [["1349", "Norway"], ["Cultes des Ghoules", "Poland"], ["Marduk", "Sweden"], ["Mayhem", "Norway"],
["Portal", "Australia"], ["Yob", "USA"]]
metal.sort_by{|band, country| country} # Returns an array of key-value pairs sorted by value
=> [["Portal", "Australia"], ["Mayhem", "Norway"], ["1349", "Norway"],
["Cultes des Ghoules", "Poland"], ["Marduk", "Sweden"], ["Yob", "USA"]]
Methods for Finding a Particular Element
The methods :first,:max, :max_by, :min, :min_by, :minmax, :minmax_by, return the element of the object satisfying the condition of the method. Please note that there is no 'last' method to return the last element, but you can usually find the last element by converting to an array and finding the last element of that array.
metal = {'Mayhem' => "Norway", '1349' => "Norway", 'Marduk' => "Sweden", 'Portal' => "Australia",
'Yob' => "USA", 'Cultes des Ghoules' => "Poland" }
metal.first # Returns an array of key-value pair corresponding to the first element of metal
=> ["Mayhem", "Norway"]
metal.min_by{|band, country| country.length}
=> ["Yob", "USA"]
metal.minmax_by{|band, country| band.length}
# Returns an array with the bands of shortest and longest name-length
=> [["Yob", "USA"], ["Cultes des Ghoules", "Poland"]]
Methods for Constructing New Objects from Other Objects
The methods :collect, :collect_concat, :drop, :drop_while, :flat_map, :inject, :map, :reject, :select, :zip create a new object based on the block of code that the method is applied to.
metal = {'Mayhem' => "Norway", '1349' => "Norway", 'Marduk' => "Sweden", 'Portal' => "Australia",
'Yob' => "USA", 'Cultes des Ghoules' => "Poland" }
metal.map {|band, country| band.to_s + " rocks " + country.to_s}
=> ["Mayhem rocks Norway", "1349 rocks Norway", "Marduk rocks Sweden", "Portal rocks Australia",
"Yob rocks USA", "Cultes des Ghoules rocks Poland"]
(5..10).inject(12) { |product, n| product * n }
# This multiplies (5..10).inject { |product, n| product * n } by 12
=> 1814400
bands = ["1349 ", "Portal ", "High on Fire "]
facts = ["rules the abyss", "opens other-dimensional gateways to madness", "melts you into the primal ooze"]
bands.zip(facts)
=> [["1349 ", "rules the abyss"], ["Portal ", "opens other-dimensional gateways to madness"],
["High on Fire ", "melts you into the primal ooze"]]