Simple question about tick'ing in a loop

I’m going through the tutorial and having a blast :slight_smile:
A question came to me while reading the section about ticking though.
In the section about with_fx I learned that it’s suboptimal to do something like

loop do
   with_fx
    ...
  end
end

because you’re creating a new fx each time through the loop.

But in the section about tick’ing this seems to not be creating a new ring each time through the loop

live_loop :arp do
  play (scale :e3, :minor_pentatonic).tick, release: 0.1
  sleep 1.0/8.0
end

Why would I not just get the first note of the scale every time in this code snippet?
It seems like the ring is re-initialized each time through the loop, and then calling tick on the new ring should just return the first note in the scale.

Really I’m just trying to build up my mental model of how this code snippet works.
If I imagine a tick as a counter that maintains state outside of the loop (like the counter variable mentioned in the tutorial), then it kind of makes sense.
Def seems magical though since the tick looks like it gets initialized in the loop!

Hi @briansorahan, welcome to the Sonic Pi forums!

Although the ring is recreated each time around the loop, the tick is completely separate and maintains its state local to the thread (a tick in another thread, or live_loop, has its own separate state).

It looks like it’s iterating through the ring, but actually it’s like a counter, and completely separate from the ring, so it doesn’t matter if you destroy and recreate the ring, it will still index into the new ring with the same offset.

You can even use the tick on its own to get the value of the counter: puts tick will print it. But watch out because it will also increment it; if you just want to use the value without incrementing it you should use look instead of tick.

I hope that clarifies things a bit. The way tick works can be a bit confusing at first, but it’s really useful once you get your head around it.