Created
January 16, 2019 20:09
-
-
Save annikoff/a03db024e26b0830c6e8bedff4820971 to your computer and use it in GitHub Desktop.
Custom array 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
class Array | |
def each(sym = nil) | |
return enum_for(:each) if !block_given? && sym.nil? | |
raise SyntaxError if block_given? && !sym.nil? | |
i = 0 | |
while i < self.length | |
sym.nil? ? yield(self[i]) : self[i].send(sym) | |
i += 1 | |
end | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment