Skip to content

Instantly share code, notes, and snippets.

@ahimmelstoss
Created October 11, 2013 18:47
Show Gist options
  • Save ahimmelstoss/6939988 to your computer and use it in GitHub Desktop.
Save ahimmelstoss/6939988 to your computer and use it in GitHub Desktop.
def my_each(array)
i = 0
while i < array.length
raise (yield(array[i])).inspect
i+=1
end
array
end
array = [1, 2, 3]
my_each(array) do |i|
i*i
end
def my_collect(array)
i = 0
collect = []
while i < array.length
collect << yield(array[i])
i+=1
end
collect
end
array = [1, 2, 3]
my_collect(array) do |i|
i*i
end
def my_select(array)
i = 0
select = []
while i < array.length
if (yield(array[i]))
select << array[i]
i+=1
end
select
end
array = [1, 2, 3]
my_select(array) do |i|
i.is_odd?
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