home/ atoms/ sonic-pi-ring-chains

Sonic Pi ring chain methods transform rings immutably, returning a new ring without modifying the original

Calling .reverse, .shuffle, .take(n), .drop(n), .stretch(n), .mirror, .sort etc. on a ring returns a new ring; the original is unchanged. Chains can be composed in sequence. Because rings are immutable, sharing a ring across threads and applying chains inside a thread is always thread-safe, unlike shared mutable variables which can cause race conditions.

Examples

r = (ring 1,2,3,4,5) puts r.shuffle.drop(1).take(3) # new ring, r still (1,2,3,4,5)

Assessment

Build a ring of 8 notes, create 3 variations using different chain combinations. Confirm the original ring is unchanged. Contrast with a plain Ruby array.

“chaining methods described in this section _do not change rings_ rather they _create new rings_”
corpus · sonic-pi-built-in-tutorial · chunk 17