Different syntax for array


# to store a value into a variable
note = 60
play note

# to store many values into a variable
notes_array = [60, 62, 64,72]

play notes_array
sleep 1

# and now a surprise for me concerning the syntax
notes = 60,62,64,72
play notes
sleep 1
play_pattern_timed notes, 1

play notes.choose
sleep 1
play notes[1]

I teach sonic pi some pupils about 12 old and i prefer to use the [] syntax but a pupil tried to separate the values with a comma and learns me it works. funny !

Is it a syntax ruby effect ?

Interesting. I’m not sure if that is standard Ruby syntax as I couldn’t find a reference to it on google. Could be, or could be Sonic Pi parsing of the buffer.
It certainly converts it to an array list, as you can see if you do.

notes = 60,62,64,72
puts notes

which produces

└─ [60, 62, 64, 72]

in the log.
Althoughg it works, I think I would encourage them to use the more usual syntax of

notes = [60, 62, 64, 72]

School kids will always teach you something :slight_smile:

yes that’s my opinion too !

It’s standard Ruby, I think it’s an implicit conversion because of the variable assignment.

puts 60,62,64,72
60 62 64 72

I learned something new today :smile:

It follows the Robustness principle - “Be conservative in what you do, be liberal in what you accept from others”

Which also means encouraging them to write the code including brackets is well founded.

1 Like

the critism part of the article is worth to be read

1 Like

I’ve definitely seen it work both ways.

It’s just an interesting thing to consider when you’re writing code, rather than a rule to adhere to.

I understand it more as a guideline for designing user interfaces.
In coding, I follow the first part.