I want to connect two Ruby enumerators. Give me all the values from the first, then the second, and so on. Ideally, without forcing any lazy evaluations and flat so I don’t have to think about nested stuff. Like so:
xs = [1, 2, 3].to_enum ys = [4, 5, 6].to_enum [xs, ys].chain.to_a # => [1, 2, 3, 4, 5, 6]
I couldn’t figure out how to do that with Ruby’s standard library alone. But, it wasn’t that hard to write my own:
def chain(*enums) return to_enum(:chain, *enums) unless block_given? enums.each { |enum| enum.each { |e| yield e } } end
But it seems like Ruby’s library, Enumerable
in particular, is so strong I must have missed something. So, mob programmers, is there a better way to do this? A fancier enumerator-combining thing I’m missing?