Skip to content

Instantly share code, notes, and snippets.

@annikoff
Created January 16, 2019 20:09
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 annikoff/a03db024e26b0830c6e8bedff4820971 to your computer and use it in GitHub Desktop.
Save annikoff/a03db024e26b0830c6e8bedff4820971 to your computer and use it in GitHub Desktop.
Custom array each method
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