Skip to content

Instantly share code, notes, and snippets.

@shirts
Created October 12, 2019 00:55
Show Gist options
  • Save shirts/3b8e1569c15d3ffaa74e6b0680fc1117 to your computer and use it in GitHub Desktop.
Save shirts/3b8e1569c15d3ffaa74e6b0680fc1117 to your computer and use it in GitHub Desktop.
Ruby Enumerable example
class ColorsEnum
include Enumerable
def initialize(colors)
@colors = colors
end
def each(&block)
block.call(@colors[0])
@colors.shift
ColorsEnum.new(@colors).each(&block) if @colors.any?
end
end
enum = ColorsEnum.new(['red', 'orange', 'yellow', 'green'])
enum.each { |color| puts color }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment