Created
October 12, 2019 00:55
-
-
Save shirts/3b8e1569c15d3ffaa74e6b0680fc1117 to your computer and use it in GitHub Desktop.
Ruby Enumerable example
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
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