So I have an array. I want to take the first elements and break them into new line. So my array is =
a = [0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
How do I print it as
00000
00000
00000
00000
00000
Thanks.
Just use the each_slice
method of the Enumerator class to split your original array into arrays that consist of five elements each and the join
method of the Array class to convert the five element arrays to strings:
a.each_slice(5) { |x|
puts x.join
}
a.each_index do |i|
puts if i%5 == 0
print a[i]
end
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