Skip to content

Instantly share code, notes, and snippets.

@ayoformayo
Last active December 18, 2015 05:59
Show Gist options
  • Save ayoformayo/5736935 to your computer and use it in GitHub Desktop.
Save ayoformayo/5736935 to your computer and use it in GitHub Desktop.
#Array#map calls the passed block once for each element of the array it was called on.
#It creates and return a new array containing the new values.
#Array#inject combines each element of an enumerator by applying the operation specified in the attached block or
#represented by an operator/method symbol (like +, -, /). If a block is specified, the block is passed an accumulator value
#and the individual element. It then returns the value of memo.
#Array#select returns a new array containing all the elements of the passed array for which the passed block is true.
class Array
def my_map
new_array = []
self.length.times do |x|
new_array << yield(self[x])
end
return new_array
end
end
end
p [1,2,3].my_map {|x| x*3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment