Created
September 27, 2013 03:52
-
-
Save davidbella/6723873 to your computer and use it in GitHub Desktop.
Ruby: Some examples of yielding to create a custom each method
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 | |
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