Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Last active August 29, 2015 14:00
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 mgreenly/11183615 to your computer and use it in GitHub Desktop.
Save mgreenly/11183615 to your computer and use it in GitHub Desktop.
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