How to get a ring index

Hello,

I have a ring array that is controlled by a rotary encoder, so it loop back round using “tick”:

notes = (ring :E4, :F4, :B4)
notes.tick
notes.tick
notes.tick
notes.tick

How can I get the current position of the current ring, so I can use it in a different ring? In this example the tick position is 0 (corresponding to E4). Is there any “get ring index” function?

Thank you very much!

Sorry, I have found the solution. It’s so simple as:

idx = tick

Thank you

For clarity, it’s important to note that .tick does not increment an index for a given ring, it increments an index for the current live_loop. This is an important but subtle distinction :slight_smile:

To look at the current value of a tick just call look or to look up the current tick value in a ring without incrementing the tick call .look.

Thanks for the clarification. Can I get a different look value for each ring?

notes = (ring :E4, :F4, :B4)
notes2 = (ring :C1, :B1, :D1)

define :loop_name do
    notes.tick
    sleep 1
end

define :loop_name2 do
    notes2.tick
    sleep 1
end

loop_name

loop_name2
loop_name2

idx = look

puts idx

Ticks are thread local, not scope or method local. So if both loop_name and loop_name2 are called within the same thread, the same tick will be incremented twice. Ticks belong to threads/live_loops.

If you wish to have more than two ticks running in parallel in the same live loop, then you need to give them different names. This is as simple as putting the name in ( ) after you write tick:

# this will tick both notes 
# and notes2 independently:
notes.tick(:foo)
notes2.tick(:bar)

Further info can be found in the Naming Ticks section of the tutorial: https://sonic-pi.net/tutorial.html#section-9-4

Ok, I undestand. In this case, how would save a specific look index?

idx = look(:foo)

This code would store the current tick value for the :foo tick to a variable called idx :slight_smile:

Excellent. Thank you!