Created
June 3, 2016 06:08
-
-
Save nicohvi/c703d1adff181150ab2fa21cc5220812 to your computer and use it in GitHub Desktop.
ruby iteration
This file contains 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
test1 = "1\n2\n3" | |
test2 = [1, 2, 3] | |
def iterate (obj) | |
method = obj.is_a?(Array) ? :each : :each_line | |
obj.send(method) do |line| | |
yield line | |
end | |
end | |
iterate (test1) do |line| | |
p line | |
end | |
# => 1\n, 2\n, 3\n | |
iterate (test2) do |line| | |
p line | |
end | |
# => 1, 2, 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment