Enumerable Flashcards
Ruby Enumerable
returns true if the block never returns false or nil
all? { |obj| block } => true or false
Ruby Enumerable
returns true if the block ever returns a value other than false or nil
any? { |obj| block } => true or false
Ruby Enumerable
returns a new array with the results of running block once for every element in enum
map { |obj| block } => array
Ruby Enumerable
returns the number of elements for which the block yields a true value
count { |obj| block } => int
Ruby Enumerable
returns the first for which block is not false
find(ifnone = nil) { |obj| block } => obj or nil
Ruby Enumerable
alias for select
find_all { |obj| block } => array
Ruby Enumerable
groups the collection by result of the block; returns a hash where the keys are the evaluated result from the block and the values are arrays of elements in the collection that correspond to the key
group_by { |obj| block } => a_hash
ex
(1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
Ruby Enumerable
returns true if any member of enum equals obj
include?(obj) => true or false
Ruby Enumerable
combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator
inject(initial, sym) => obj inject(initial) { |memo, obj| block } => obj
ex
(5..10).inject(0, &:+) => 45 (5..10).inject(0) { |memo, num| memo + num } => 45
Ruby Enumerable
returns the object in enum with the maximum value; can use a block to implement a \<=\> b
max(n) { |a,b| block } => obj
ex
a = %w(albatross dog horse)
a. max #=> “horse”
a. max { |a, b| a.length b.length } #=> “albatross”
Ruby Enumerable
alias of include?
member?(obj) => true or false
Ruby Enumerable
opposite of max
min(n) { |a,b| block }
Ruby Enumerable
returns true if the block never returns true for all elements
none? { |obj| block } => true or false
Ruby Enumerable
returns true if the block returns true exactly once
one? { |obj| block } => true or false
Ruby Enumerable
opposite of select
reject { |obj| block } => array