Perusing Ruby Enumerable - Part 1
-
all? Returns true or false depending on the results of iterating through the collection. If there is a nil or false in the collection, all? will return false - obviously. If there is no block, ruby adds an implicit
{ |obj| obj }
which returns true. -
any? Here’s a gotcha - it will return true if ANY of the elements are not false or nil.
-
collect - Returns a new array.
-
collect_concat == flat_map
-
count - Counts number of items in a collection. Add an argument to count the number of said argument in the collection. If a block, the number of elements that yield true are counted.
-
cycle - potential for infinity. Takes an argument that defaults to nil (cycle forever).
-
find - takes an argument that will return it’s result if no object matches the enumerable. Gotcha - need to call argument with a lambda otherwise you’ll get an error.
-
drop/drop while - Drops the
n
element in a collection. Does exactly what you think it does. -
each_cons - returns a consecutive
n
of elements. Weird one here, can’t think of any uses for it, yet. -
each_slice - immediately seems useful. It iterates through the collection and returns an array (or slice rather) of n elements.
-
each_with_index - So this enumerable allows you to write a block that iterates through the element indexes in your collection. There is potential business use for this, I can already tell.
-
each_with_object - Seems like a better way to create hashes as opposed to inject. Will have to play with this one.