Mehackit tutorial - question about rings and ticks

I’m working my way through this tutorial (as a complete novice ) and have got a bit confused with this example from the section on rings and ticks What exactly is the ring function bringing to the party here, as it seems to make no difference if I remove it?

live_loop :keys do
use_synth :blade
play [(chord :Ab, :major7), (chord :Db, :major7), (chord :Bb, :minor7), (chord :Eb, ‘7’)].ring.tick, attack: 0.1, release: 1
sleep 1
end

If you’re using .tick, it doesn’t make a difference because .tick treats arrays as if they were rings. But if you try to access the elements by index, then you will see a difference. For example:

data = [(chord :Ab, :major7), (chord :Db, :major7), (chord :Bb, :minor7), (chord :Eb, '7')]

puts "Without ring"
range(0, 8).each do |i|
  puts i, data[i]
end

puts "With ring"
range(0, 8).each do |i|
  puts i, data.ring[i]
end

The first loop prints:

"Without ring"
0.0 (ring 68, 72, 75, 79)
1.0 (ring 61, 65, 68, 72)
2.0 (ring 70, 73, 77, 80)
3.0 (ring 63, 67, 70, 73)
4.0 nil
5.0 nil
6.0 nil
7.0 nil

But the second loop prints:

"With ring"
0.0 (ring 68, 72, 75, 79)
1.0 (ring 61, 65, 68, 72)
2.0 (ring 70, 73, 77, 80)
3.0 (ring 63, 67, 70, 73)
4.0 (ring 68, 72, 75, 79)
5.0 (ring 61, 65, 68, 72)
6.0 (ring 70, 73, 77, 80)
7.0 (ring 63, 67, 70, 73)

You can see in the first loop when it gets to the end of the array it just gets nil, but in the second loop in loops back around to the start, even when you get elements by index.

1 Like