Ruby Symbols in SPi

As a Ruby noob, I’ve seen symbols and read about them - they seem to be described as kind of mystic. Something nebulous that Ruby coders feel rather than can explain. Hmmm OK.

Well. :C4 is symbol that has a string and a number associated with it (or whatever the phrase of the day is). Seems fine to me

How do you create your own in SPi? I want to have something similar for note values

e.g. :crotchet means “crotchet” and a floating point number 1.0/4

From the gnostic descriptions, it seems to me that a symbol ought to be associated with an object that could have any attributes you like, but maybe that’s for another day. The talk is of a string and a number.

Hi,

A Ruby symbol is just an immutable string that always has the property that :foo is guaranteed to always be the same object as :foo.

In some Object Oriented languages the string "foo" is not necessarily the same object as "foo". It’s kind of like having two identical books sitting side-by-side on a table. They have the same contents, but they’re not the same book.

However, in Ruby, the symbol :foo is always the same object as any other :foo - and that’s guaranteed by the language. This means that it’s possible (and recommended) to use symbols as keys to lookup tables.

In Sonic Pi we use symbols to refer to names of things - live loops, scales, chords, FX, etc.

1 Like

Thanks Sam. That’s really what I thought from reading the articles, although as I say the ones I read all suggested there was some deeper thing going on.

But the question remains, what’s the way in Sonic Pi to make my own :crotchet = “crotchet” and 1/4?

If I do

puts :c4.to_s
puts :c4.to_f

the output is

{run: 5, time: 0.0}
 ├─ "c4"
 └─ 60.0

And I want to define my own like that. to_s is a getter, not a setter so it’s not that. I’m good with hashes (like this) so is it something around that?

thing = {:foo => 3, :bar => 4}

This is because Sonic Pi is not Ruby. This is something I try to emphasise but often seems to get pushed to the wayside. Sonic Pi is built on top of Ruby (currently) and therefore shares a lot of behaviour, but it also changes behaviour (often in subtle places) towards its goal of being a musical instrument and not a Ruby library.

Having :c4 know how to turn itself to the MIDI number 60 means that :c4 + 1 works by returning the MIDI note for :cs4. This isn’t something a typical programming language will do :slight_smile:

Also, calls like to_s and to_f are really not supported and you shouldn’t assume they’ll always be available and/or produce the same behaviour across versions.

I’d definitely look towards using hashes/maps such as {:foo => 3, :bar => 4} this is something I do hope to continue to support as the language evolves.

OK, got it. Sorry, hope I’ve not touched a raw nerve there. I like to go with the grain with these things, and seemed that using Ruby constructs was the grain. I read that symbols were a big thing in Ruby culture, so wanted to use them. I don’t need to though. Likewise hashes.

1 Like

Reflecting a bit more on this…, I’ve read that you’re aiming it at education and pro use (or ‘other than schools’, more serious use, something like that?). That one of the features that help get it adopted in education is that it’s like Ruby which is like Python. I don’t know which for you is the main driver at the moment.

I’ve got no axe to grind on the education side, so purely as someone making music I don’t care what the language is, as long as it provides the functionality. Also I don’t care very much if you change specific features in between versions. I know people do get het up that stuff but IMO that’s a hinderance to progress, a load of extra work and the tail wagging the dog.

For me, it’s a bit of fun looking at Ruby but that’s not why I’m using SPi. I may be in a minority of one (bet I’m not), but think it’s important to make the case because in my experience the people obsessing about backwards compatibility tend to be a lot more vocal.

1 Like

No worries at all. Whilst I do care about backwards compatibility and listen to those very vocal in favour of it, it’s not more important than progress in my eyes. I’m therefore very capable of moving forward where I think makes the most sense and dealing with the discomfort afterwards :wink: