Skip to content

Instantly share code, notes, and snippets.

@faizaanshamsi
Created May 29, 2014 13:35
Show Gist options
  • Save faizaanshamsi/78b7f1094d681a9d6b21 to your computer and use it in GitHub Desktop.
Save faizaanshamsi/78b7f1094d681a9d6b21 to your computer and use it in GitHub Desktop.
Each and Map
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