Ruby array repeat value if next not present,

arrays, ruby, ruby-on-rails

Solution

You have the abstractions you need in the core, just wire them together: Enumerable#cycle, Enumerable#take and Enumerable#inject:

>> [1, 2, 3].cycle.take(5).inject(0, :+)
=> 9

That's the functional/declarative approach: use abstractions (either existing or those you implement yourself) so you can write code that describes what you are doing instead of how you are doing it.

Problem

How can we achieve this in Ruby? ``` xs = [1,2,3] x = 5 ``` Then I need that `sum = 1+2+3+1+2 = 9`

Original source