Search code examples
pythonarraysrubylanguage-comparisons

Ruby equivalent to Python chain()


What is the Ruby equivalent of the chain iterator in python?

data_chained = []
data2 = {}     
data_chained = chain(data_chained, data2)

How can this be done in Ruby?


Solution

  • Since Ruby 2.6: if it is Enumerable, you can chain it: (example from the docs, chaining a Range to an Array)

    e = Enumerator::Chain.new(1..3, [4, 5]) 
    e.to_a #=> [1, 2, 3, 4, 5]
    e.size #=> 5