What does this do

4.times do
melody =
scale_new = [e_penta, e_penta, e_phrygian].choose
i = [0, rrand_i(0, 100)].choose
n = rhythm.size
n.times do
melody = melody << scale_new[i]
i = i + [-2,-1,-1, 1,1,2, rrand_i(0,100)].choose
end
#play
i = 0
n = rhythm.size
n.times do
play melody[i], sustain: rand, amp: rrand(0.1, 0.3)
sleep rhythm[i]
i = i + 1
end
end

the melody = melody << scale_new[i] what does it do?

Hi Ase. welcome to in-thread.
Your code seems to be rather concept in nature as it is not runnable in its present form on Sonic Pi. For a start you need a list containing the rhythm of the notes you want to play. Secondly the scale names are not compatible with what is available in Sonic Pi, and you need to generate a list of notes for each scale that you choose.

First in answer to your question the << in Ruby will append the list on the right hand side to the existing list on the left. In other words each time the n.times loop runs, you add a note from the scale to the melody.

I have take the liberty of re-wrting the code so that it will work on Sonic Pi. I’ve added a rhtythm of eight notes, and changed the scale names to three scales existing in Sonic Pi. I’ve also generated notes for each of these scales when they are selected. I’ve set the synth to :tri and raised the bpm to 180 so it plays at a reasonable rate. I’ve also added a puts statement to print out the increasing melody on each path of the loopI hope this give you enough to develop your ideas further.

NB if you post code in between two lines each containing three ```
then it will format nicely.

#amended code by Robin Newman which should run OK on Sonic Pi
rhythm=[1,1,1,0.5,0.5, 1,1,0.5,0.5,1]
use_bpm 180
use_synth :tri
4.times do
  melody = []
  scale_new = scale(:c4,[:major_pentatonic, :minor_pentatonic, :phrygian].choose)
  puts scale_new
  i = [0, rrand_i(0, 100)].choose
  n = rhythm.size
  n.times do
    melody = melody << scale_new[i]
    puts "increasing melody",melody
    i = i + [-2,-1,-1, 1,1,2, rrand_i(0,100)].choose
  end
  #play
  i = 0
  n = rhythm.size
  n.times do
    play melody[i], sustain: rand, amp: rrand(0.1, 0.3)
    sleep rhythm[i]
    i = i + 1
  end
end