Created
October 1, 2013 20:41
-
-
Save davidbella/6784752 to your computer and use it in GitHub Desktop.
Ruby: Creating iterators
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
def my_each(array) | |
i = 0 | |
while i < array.length | |
yield(array[i]) | |
i+=1 | |
end | |
array | |
end | |
def my_collect(array) | |
i = 0 | |
collect = [] | |
while i < array.length | |
collect << (yield(array[i])) | |
i+=1 | |
end | |
collect | |
end | |
def my_select(array) | |
i = 0 | |
select = [] | |
while i < array.length | |
if (yield(array[i])) | |
select << array[i] | |
end | |
i+=1 | |
end | |
select | |
end | |
def my_none?(array) | |
i = 0 | |
while i < array.length | |
if (yield(array[i])) | |
return false | |
end | |
i += 1 | |
end | |
true | |
end | |
array = [1,2,3] | |
my_none?(array) do |i| | |
i.is_a?(String) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment