Def vs define: symbols and scope + What even is the Sonic Pi language?

Sonic Pi has led to me learning the Ruby language itself.
I’ve found that these are mostly the same:

define :hf9 do 69 end

play hf9

(The semicolons are a way to not have to create a new line.)

def hf9; 69; end    

play hf9

However, in the second case, the function is being declared with its own scope. If there were references to variables within the def block then that would matter.

I also think there might be a difference in whether the name hf9 is a symbol or not, but I’m not sure.

How am I doing, and are there any clarification someone can point out?

1 Like

Another thought: Is there a difference between define and define_method?

I think define is the only one that’s officially supported by Sonic Pi. The others might happen to work for now but could stop working in a future version.

Does this mean that the language used by Sonic Pi is more like a fork of Ruby rather than a library of it?
Also, is define unique to Sonic Pi?

I’m not sure about the details, but my understanding is that it’s currently built on top of Ruby, so a lot of Ruby constructs happen to work, but that may change in the future. All that is guaranteed to work is what is documented in the tutorial and the lang section of the docs.
I believe define is specific to Sonic Pi.

Thanks.
From my perspective, it would be better to keep most of the Ruby in Sonic Pi. This way we have more flexibility and can more easily learn some Ruby along the way.

Just happened to find another notable difference: When an error occurs, Sonic Pi will trace it down to the line of its actual cause in the case of define, but will just point at the function call in case of def, making debugging harder.

2 Likes

Hi all, joining the party to hopefully help and get help!
I’m learning so much; how are you managing all this? I’m almost tempted to blog (started a thread to track my progress, but getting a little distracted with tangents! the onion has many layers…

  • define is not unique to sp, which isn’t a fork, more of an awesome ruby wrapper of supercollider, maybe a subset of both ruby and sc … please correct me if I’m inaccurate @ethancrawford or any other :cat:‍:bust_in_silhouette:

  • I think this is where the ‎SonicPi::Lang::Core::define‎ method is… defined, using the ruby def command… which is like an alias to define… I think!

module ERB::DefMethod - Documentation for Ruby 3.3 (ruby-lang.org)

Hope this helps!

:bulb: Should we collaborate on a pibook hosted on git? :woman_shrugging:
If anyone is interested, what chapters and topics shall we cover?