In this case the Ruby “power bits” are contained in the function beat1 and in the use of hashes.
The former can be understood if you break it down into its constitutuent parts.
The following program does this in 5 stages.
When you run it you will see the effect of each additional stage. The various stages are linked together by a . which means apply the following method to what has gone before.
You see this for example when .tick or .look are applie or .choose and .shuffle to mention but a few.
In the original program listing the function is laid out over several lines. I have here presented it linearly, adding stages for each new test function. You can see the effect in the printed outputs.
define :test1 do |pat|
(ring *pat)
end
define :test2 do |pat|
(ring *pat.split(" "))
end
define :test3 do |pat|
(ring *pat.split(" ").map{|bar| bar.split("")})
end
define :test4 do |pat|
(ring *pat.split(" ").map{|bar| bar.split("")}.flatten)
end
define :test5 do |pat|
(ring *pat.split(" ").map{|bar| bar.split("")}.flatten.map{|d| d=="1" ? 0 : 1})
end
puts "test 1 does this", (test1 "1--- 1--- 1--- 1---")
puts "test 2 does this", (test2 "1--- 1--- 1--- 1---")
puts "test 3 does this", (test3 "1--- 1--- 1--- 1---")
puts "test 4 does this", (test4 "1--- 1--- 1--- 1---")
puts "test 5 does this", (test5 "1--- 1--- 1--- 1---")
If you follow through the various stages you will see the effect of the .split and .map and .flatten methods.
The hash function can be thought of as setting up a lookup list. Thus for the hash inst0 you can use the lookup keys :k0 :s0 and :h0 to retrieve the associated sample name.
Although, as you show, you can write out the program in standard SP syntax, The advantage of the beat1 function is that it enables you visually to see the rhythms very easily, and also you can easily try out different rhythmic patterns without having to rewrite the code, only alter the data strings.
However, as Sam says, you use Ruby constructs at your own risk, and what works today may not necessarily work in future versions. Also it can make it difficult to follow exactly what a program is doing.