Skip to content

Instantly share code, notes, and snippets.

@miyucy
Last active August 10, 2018 15:16
Show Gist options
  • Save miyucy/5d3056605cd3da54967dda9b53c7a82e to your computer and use it in GitHub Desktop.
Save miyucy/5d3056605cd3da54967dda9b53c7a82e to your computer and use it in GitHub Desktop.
class Concatenator
def self.[](*objs)
new(*objs)
end
def initialize(*objs)
@objs = objs
end
def each(&blk)
return to_enum unless block_given?
eeach([*@objs], &blk)
end
private
def eeach(objs, &blk)
return if objs.empty?
objs.shift.each(&blk)
eeach(objs, &blk)
end
end
__END__
c = Concatenator[[1, 2, 3], %w[1, 2, 3], %i[1 2 3], [1, 2, 3].each]
c.each # => Enumerator
c.each do |e|
p e
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment