Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
Created September 27, 2013 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanbrennan/6724324 to your computer and use it in GitHub Desktop.
Save ivanbrennan/6724324 to your computer and use it in GitHub Desktop.
Handmade yield
def my_each(array)
empty = (array.length == 0)
i = 0
while (i < array.length) && !empty
yield array[i]
i += 1
end
array
end
sea_array = ["turtle", "shark", "whale", "eel"]
num_array = [5,2,6,1,0]
empty_array = []
puts "sea_array = #{sea_array.inspect}"
puts "my_each(sea_array) {|creature| puts creature}:"
my_each(sea_array) {|creature| puts creature}
puts
puts "sea_array.each {|creature| puts creature}:"
sea_array.each {|creature| puts creature}
puts
puts "num_array = #{num_array.inspect}"
puts "my_each(num_array) {|num| puts (num * 10)}:"
my_each(num_array) {|num| puts (num * 10)}
puts
puts "num_array.each {|num| puts (num * 10)}:"
num_array.each {|num| puts (num * 10)}
puts
puts "empty_array = #{empty_array.inspect}"
puts "my_each(empty_array) {|element| puts element}:"
my_each(empty_array) {|element| puts element}
puts
puts "empty_array.each {|element| puts element}:"
empty_array.each {|element| puts element}
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment