Skip to content

Instantly share code, notes, and snippets.

@davidbella
Created September 27, 2013 03:52
Show Gist options
  • Save davidbella/6723873 to your computer and use it in GitHub Desktop.
Save davidbella/6723873 to your computer and use it in GitHub Desktop.
Ruby: Some examples of yielding to create a custom each method
def my_each (array)
i = 0
while i < array.length
yield array[i]
i += 1
end
end
my_each [1, 2, 3, 4, 5] { |i| puts i**2 }
class Array
def my_each
i = 0
while i < self.length
yield self[i]
i += 1
end
end
end
[1, 2, 3, 4, 5].my_each { |i| puts i**2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment