Skip to content

Instantly share code, notes, and snippets.

@davidbella
Created October 1, 2013 20:41
Show Gist options
  • Save davidbella/6784752 to your computer and use it in GitHub Desktop.
Save davidbella/6784752 to your computer and use it in GitHub Desktop.
Ruby: Creating iterators
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