Call global function from within struct

not sure if this is a SonicPi or rather a Ruby question, but suppose I have a struct named Pattern like so:

Pattern =
:sample_idx) do
  def play
    sample self.sample_dir, self.sample_xp, self.sample_idx

If I instantiate a pattern and call play on it:

my_pattern = 0,1), 8, hats, "", 6)

I get

 undefined method `sample' for #<struct SonicPi::RuntimeMethods::Pattern

How can I call sample from within the struct?


Hi @sancho1980 - neither Struct or def are supported in Sonic Pi.

It’s important to note that only the language concepts described in the tutorial are supported. There are many features of Ruby (they underlying implementation language) which are available which may or may work, but as you’re discovering, there can definitely be issues attempting to combine non-supported concepts with the internal functionality such as sample.

There are plans to port Sonic Pi’s language features to Elixir, so when that happens Struct will not be available at all.

Hi @samaaron ,
that’s too bad, I’m starting to write a step sequencer with Sonic Pi, and without more complex data types like structs, what I have in mind is not going to be possible. For the moment, everything that I need seems to work. For example, as regards my question in this thread, all I had to do was add another parameter (named outer_scope) to my “play” function and pass “self” for it. Then calling “outer_scope.sample” works from within my play function.

What are the reasons for this switch to elixir? I guess all compositions will have to be rewritten then. Will the port to Elixir then support all language features, or will also only a small subset of the language be supported?



@sancho1980, I was told the same thing 4 years ago. Did not stop me either. Could not have done my numeric notation sequencer (Ziffers) using just Sonic Pi’s methods. To my knowledge there is no other way to extend Sonic Pi’s capabilities at this moment than use Ruby.

For sure, the change is coming … :hear_no_evil: … but i’ll keep monkeypatching while I can :slight_smile:

@samaaron, Still hoping that there would be a way to run unofficial Ruby extensions also in the future … or i’ll use a fork :see_no_evil:.