Skip to content

Instantly share code, notes, and snippets.

@xta
Created October 25, 2012 19:08
Show Gist options
  • Save xta/3954749 to your computer and use it in GitHub Desktop.
Save xta/3954749 to your computer and use it in GitHub Desktop.
Enumerable Ruby Methods
#For processing iterations
Array#each
Calls block once for each element in self, passing that element as a parameter.
====================================================================================
#Return array / object after iterations
Enumerable#map / Enumerable#collect
Returns a new array with the results of running block once for every element in enum.
If no block is given, an enumerator is returned instead.
Object#tap
Yields x to the block, and then returns x. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.
====================================================================================
#Opposites
Enumerable#reject
Returns an array for all elements of enum for which block is false (see alsoEnumerable#find_all).
Enumerable#select
Returns an array containing all elements of enum for which block is not false (see alsoEnumerable#reject).
====================================================================================
#Equivalent, different order for pipes
Enumerable#each_with_object
Iterates the given block for each element with an arbitrary object given, and returns the initially given object.
[1, 2, 3, 4].each_with_object([]) { |element, result| result << element }
Enumerable#inject
Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.
[1, 2, 3, 4].inject([]) { |result, element| result << element }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment