I have two arrays of integers, e.g.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [7, 8, 9]
I would like to repeatedly duplicate the value of 'b' to get a perfectly matching array lengths like this:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [7, 8, 9, 7, 8, 9, 7, 8, 9, 7]
We can assume that a.length > b.length
Assuming you mean
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [7, 8, 9]
then you can do:
b.cycle.take(a.length) #=> [7, 8, 9, 7, 8, 9, 7, 8, 9, 7]
<script src="//repl.it/embed/JJ3x/2.js"></script>
See Array#cycle and Enumerable#take for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With