Skip to content

Instantly share code, notes, and snippets.

Created January 5, 2016 22:03
Show Gist options
  • Save anonymous/2129eb4f3af54a3617e2 to your computer and use it in GitHub Desktop.
Save anonymous/2129eb4f3af54a3617e2 to your computer and use it in GitHub Desktop.
Add a my select method to the Array class
class Array
public
def my_each
for i in self
yield(i)
end
self
end
def my_each_with_index
i = 0
self.my_each do |element|
yield(element, i)
i += 1
end
end
def my_select
result = []
self.my_each do |element|
result << element if yield(element)
end
result
end
# [1, 2, 3].my_select do |item|
# item <= 2
# end
end
#you can either write this in irb or pry
require './my_select.rb'
[1, 2, 3].my_select do |item|
item <= 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment