Created
January 5, 2016 22:03
-
-
Save anonymous/2129eb4f3af54a3617e2 to your computer and use it in GitHub Desktop.
Add a my select method to the Array class
This file contains 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
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 |
This file contains 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
#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