Chautauqua Logo

Ruby module Enumerable interactive

Prompt icon → Click to get an interactive sample (type in code)

Methods

Querying

include? (alias member?)(1..4).include?(2) # true Documentation Interactive samplePlayground
true
all?[1,2,nil].all?; (1..4).all?(Float) # false Documentation Interactive samplePlayground
true
any?[1,2,nil].any?; [1,2,3.0].any?(Float) # true Documentation Interactive samplePlayground
true
none?(1..4).none?; [1,2.0].none?(Float) # false Documentation Interactive samplePlayground
false
one?(1..1).one?; [1,2.0].one?(Integer) # true Documentation Interactive samplePlayground
false
count(1..3).count; [0,1,1,2].count(1) # 3;2 Documentation Interactive samplePlayground
4
tally%w[a b b a b].tally # {'a'=>2,'b'=>3} Documentation Interactive samplePlayground
{"a" => 2, "b" => 3, "d" => 2, "e" => 1}

Fetching

Leading, trailing, or all elements:
to_a (alias entries)(0..3).to_a # [0,1,2,3] Documentation Interactive samplePlayground
[0, 1, 2, 3]
first(4..9).first # 4 Documentation Interactive samplePlayground
4
take(4..9).take(3) # [4,5,6] Documentation Interactive samplePlayground
[4, 5, 6]
drop(4..9).drop(3) # [7,8,9] Documentation Interactive samplePlayground
[4, 5, 6]
take_while(4..9).take_while{|n| n < 7} # [4,5,6] Documentation Interactive samplePlayground
[4, 5, 6]
drop_while(4..9).drop_while{|n| n < 7} # [7,8,9] Documentation Interactive samplePlayground
[7, 8, 9]
Minimum and maximum value elements:
min[7,5,3,9].min; [7,5,3,9].min(2) # 3;[3,5] Documentation Interactive samplePlayground
[3, 5]
max[7,5,3,9].max; [7,5,3,9].max(2) # 9;[9,7] Documentation Interactive samplePlayground
[9, 7]
minmax[7,5,3,9].minmax # [3,9] Documentation Interactive samplePlayground
[3, 9]
min_by(1..4).min_by {|e| -e} # 4 Documentation Interactive samplePlayground
4
max_by(1..4).max_by {|e| e} # 4 Documentation Interactive samplePlayground
4
minmax_by(1..4).minmax_by {|e| -e} # [4,1] Documentation Interactive samplePlayground
[4, 1]
Groups, slices, and partitions:
group_by(1..3).group_by{|i|i/3} # {0 =>[1,2],1=>[3]} Documentation Interactive samplePlayground
{1 => [1, 4], 2 => [2, 5], 0 => [3, 6]}
partition(1..4).partition{|i|i.even?} # [[2,4],[1,3]] Documentation Interactive samplePlayground
[[2, 4, 6], [1, 3, 5]]
slice_after%w[a b c].slice_after(/a/) # [[a],[b,c]] Documentation Interactive samplePlayground
[["foo", "bar"], ["fop", "for", "baz"], ["fob"]]
slice_before%w[a b c].slice_before(/c/) # [[a,b],[c]] Documentation Interactive samplePlayground
[["foo"], ["bar", "fop", "for"], ["baz", "fob"]]
slice_when(1..3).slice_when{|i|i<2} # [[1],[2,3]] Documentation Interactive samplePlayground
[[1], [2], [3, 4, 5, 6]]
chunk(0..1).chunk{|i|(i/3)} # [[0,[0,1]]] Documentation Interactive samplePlayground
[[0, [0, 1, 2]], [1, [3]]]
chunk_while(0..3).chunk_while {|i|i<2} # [[0,1,2],[3]] Documentation Interactive samplePlayground
[[0, 1, 2], [3], [4]]

Searching and Filtering

find (alias detect)(1..6).find{|e|e>2} # 3 Documentation Interactive samplePlayground
3
find_all (select filter)(1..6).find_all{|e|e>2} # [3,4,5,6] Documentation Interactive samplePlayground
[3, 4, 5, 6]
find_index (1..6).find_index{|e|e>2} # 1 Documentation Interactive samplePlayground
2
reject(1..6).reject{|e|e>2} # [1,2] Documentation Interactive samplePlayground
[1, 2]
uniq%w[a b b c a a].uniq # ['a','b','c'] Documentation Interactive samplePlayground
["a", "b", "c"]

Sorting

sort%w[c a b].sort # ['a','b','c'] Documentation Interactive samplePlayground
["a", "b", "c", "d", "g", "z"]
sort_by%w[aa a].sort_by{|s|s.size} # ['a','aa'] Documentation Interactive samplePlayground
["a", "aa", "aaa", "aaaa"]

Iterating

each_entry(1..3).each_entry{|e|p e} # 1;2;3 Documentation Interactive samplePlayground
[1, 2, 3]
each_with_index(1..3).each_with_index{|e|p e} # 0;1;2 Documentation Interactive samplePlayground
[0, 1, 2]
each_with_object[1,2].each_with_object([]){|i,a|a<<i} # [1,2] Documentation Interactive samplePlayground
[1, 4, 9]
each_slice(1..4).each_slice(3){|t|a<<t} # [[1,2,3],[4]] Documentation Interactive samplePlayground
[1, 4, 9]
each_cons(1..3).each_cons(2){|t|a<<t} # [[1,2],[2,3]] Documentation Interactive samplePlayground
[[1, 2, 3], [2, 3, 4]]
each_each(1..3).reverse_each{|t|a<<t} # [3,2,1]] Documentation Interactive samplePlayground
[4, 3, 2, 1]

Other Methods

map (alias collect)(0..3).map{|i|i*i} # [0,1,4,9] Documentation Interactive samplePlayground
[0, 1, 4, 9, 16]
filter_map(0..4).filter_map{|i|i if i.even?} # [0,2,4] Documentation Interactive samplePlayground
[0, 4, 8, 12, 16]
flat_map (collect_concat[[1],[3,4]].flat_map{|e|e+[9]} # [1,9,3,4,9] Documentation Interactive samplePlayground
[0, 1, 100, 2, 3, 100]
grep['dig','big','ox'].grep(/ig/) # ['dig','big'] Documentation Interactive samplePlayground
["dig", "big"]
grep_v['dig','big','ox'].grep_v(/ig/) # ['ox'] Documentation Interactive samplePlayground
["bee"]
inject (alias reduce)['di',' ','do' ].inject('',:concat) # 'di do' Documentation Interactive samplePlayground
The result is: bee dog
sum(1..10).sum # 55 Documentation Interactive samplePlayground
60
zip[1,2].zip([3,4],[5,6]) # [[1,3,5],[2,4,6]] Documentation Interactive samplePlayground
[[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
cyclea=[];(1..2).cycle(2){|e|a<<e} # [1,2,1,2] Documentation Interactive samplePlayground
[1, 2, 3, 1, 2, 3, 1, 2, 3]