More Common Enumerable’s
- .all?–return ‘true’ when all elements result in true when passed into the block.
- Ex: p [2,4,6].all? { |el| el.even? } # =>(returns) true
- Ex: p [2,3,6].all? { |el| el.even? } # => false
- .any?–return ‘true’ when at least one element results in true when passed into the block
- Ex: p [3,4,7].any? { |el| el.even? } # => true
- Ex: p [3,5,7].any? { |el| el.even? } # => false
- .none?–return ‘true’ when no elements result in true when passed into the block.
- Ex: p[1,3,5].none? { |el| el.even? } # => true
- Ex: p[1,4,5].none? { |el| el.even? } # => false
- .one?–return ‘true’ when exactly one element results in true when passed into the block.
- Ex: p[1,4,5].one? { |el| el.even? } # => true
- Ex: p[1,4,6].one? { |el| el.even? } # => false
- Ex: p[1,3,5].one? { |el| el.even? } # => false
- .count–return a number representing the count of elements that result in ‘true’ when passed into the block.
- Ex: p[1,2,3,4,5,6].count { |el| el.even? } # => 3
- Ex: p[1,3,5].count { |el| el.even? } # => 0
- .sum–return the total sum of all elements.
- .max & .min–return the maximum or minimum element.
- Ex: p[1,-3,5].min # => -3
- Ex: p[1,-3,5].max # => 5
- Ex: p[ ].max # => nil
- .flatten–return the 1 dimensional version of any multi-dimensional array.
- Ex: multi_d = [ [ [ “a”, “b” ], “c” ], [ [ “d” ], [ “e” ] ], “f” ]
- [ multi_d.flatten # => [ “a”, “b”, “c”, “d”, “e”, “f” ]