Select, Reject, Collect, Detect, Inject
I’ve recently learned those convenient functions that apply on any array, or just basically any enumerable, all these functions take a block and according to this block’s output the return is decided, those methods are select, reject, collect, detect, inject.
##Select Selects items from the array if the block returns true example : we want to select the even numbers only
##Reject Same as select but rejects the items that return true from the block example: we will use the same example to reject even numbers
##Collect This is quite similar to select but instead it collects the results of the blocks instead of the array them selves. example: we want an array that contains the squared numbers of the current array
##Detect Detect finds the first ( and only the first ) item that satisfies the conditon inside the block, and returns the item’s value
##Inject Inject is one of my favorite functions, it iterates on all items and accumulates the block’s results into an accumelator, then returns that accumelator’s value, it also can take an initial value for the accumulator, which could be either an integer or any object, like an array for example
I would like to thank Matthew for his blog post as that was where I initially learned about the usage those functions