Created
May 29, 2014 13:35
-
-
Save faizaanshamsi/78b7f1094d681a9d6b21 to your computer and use it in GitHub Desktop.
Each and Map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
array = [1,2,3] | |
class Array | |
def hamburger(&block) | |
i = 0 | |
while i < self.length | |
yield(self[i]) | |
i += 1 | |
end | |
return self | |
end | |
def cheeseburger(&block) | |
result = [] | |
i = 0 | |
while i < self.length | |
result << yield(self[i]) | |
i += 1 | |
end | |
return result | |
end | |
end | |
new_array = array.cheeseburger { |x| x ** 2 } | |
puts new_array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment