Last active
August 29, 2015 14:00
-
-
Save mgreenly/11183615 to your computer and use it in GitHub Desktop.
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 Foo | |
attr_reader :start, :stop | |
def initialize(start, stop) | |
@start, @stop = start, stop | |
end | |
def each | |
return enum_for(:each) unless block_given? | |
start.upto(stop) do |count| | |
yield count | |
end | |
end | |
def each_triple | |
return enum_for(:each_triple) unless block_given? | |
p = c = n = nil | |
each do |foo| | |
p, c, n = c, n, foo | |
next if p.nil? && c.nil? | |
yield [p, c, n] | |
end | |
if n.nil? | |
yield [] | |
else | |
yield [c, n, nil] | |
end | |
end | |
end | |
# example | |
foos = Foo.new(0, 7) | |
foos.each_triple do |t| | |
puts t.inspect | |
end | |
# [nil, 0, 1] | |
# [0, 1, 2] | |
# [1, 2, 3] | |
# [2, 3, 4] | |
# [3, 4, 5] | |
# [4, 5, 6] | |
# [5, 6, 7] | |
# [6, 7, nil] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment