Ruby module Enumerable interactive
→ Click to get an interactive sample (type in code)
Methods
Querying
tally%w[a b b a b].tally # {'a'=>2,'b'=>3} Documentation Playground
{"a" => 2, "b" => 3, "d" => 2, "e" => 1}Fetching
Leading, trailing, or all elements:
Minimum and maximum value elements:
Groups, slices, and partitions:
group_by(1..3).group_by{|i|i/3} # {0 =>[1,2],1=>[3]} Documentation Playground
{1 => [1, 4], 2 => [2, 5], 0 => [3, 6]}partition(1..4).partition{|i|i.even?} # [[2,4],[1,3]] Documentation Playground
[[2, 4, 6], [1, 3, 5]]slice_after%w[a b c].slice_after(/a/) # [[a],[b,c]] Documentation Playground
[["foo", "bar"], ["fop", "for", "baz"], ["fob"]]slice_before%w[a b c].slice_before(/c/) # [[a,b],[c]] Documentation Playground
[["foo"], ["bar", "fop", "for"], ["baz", "fob"]]chunk_while(0..3).chunk_while {|i|i<2} # [[0,1,2],[3]] Documentation Playground
[[0, 1, 2], [3], [4]]Searching and Filtering
Sorting
Iterating
each_cons(1..3).each_cons(2){|t|a<<t} # [[1,2],[2,3]] Documentation Playground
[[1, 2, 3], [2, 3, 4]]Other Methods
flat_map (collect_concat[[1],[3,4]].flat_map{|e|e+[9]} # [1,9,3,4,9] Documentation Playground
[0, 1, 100, 2, 3, 100]inject (alias reduce)['di',' ','do' ].inject('',:concat) # 'di do' Documentation Playground
The result is: bee dogzip[1,2].zip([3,4],[5,6]) # [[1,3,5],[2,4,6]] Documentation Playground
[[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]