Comprehensive methods list

Is there somewhere with a comprehensive list of methods that are available for SP data structures? I’m thinking explicitly of list and ring methods, but if there are other data structures I’m forgetting, I’d love to know what I can do with them?

There isn’t a specific list like this, but chapter 8 of the inbuilt tutorial section gives lots of examples of the various data structures. You can also use ruby-doc.org to look up the array class and associated methods that can be applied However remember that Sonic Pi is NOT pure ruby and things which are not documented explicitly in Sonic Pi may not work. Other good sources are to look at programs that users post here in in-thread. I have learned a lot in this way, and publish many of my own programs here.

I’d dearly love such a method list too.
I’ve tried the ruby array method .delete_if, but it’s not supported in sonic pi.
It’s very frustrating to have such uneven documentation when learning a new programming language.

Hi Harry
as a non-programmer I have found that a combination of the Help/Lang listing in Sonic Pi, this site: Class: Array (Ruby 2.7.0) and this very forum help me get my head around what should, will, and will not work in Sonic Pi. Sonic Pi isn’t a new programming language, it’s a DSL (based on Ruby). Sam, the core team and our friends here will definitely help you solve any challenges.

The ability to ‘browse similar topics’ on this site is quite revealing too.

PD-Pi

Yes, I’ve been scouring the ruby docs and tutorials. I’m new to ruby as well as to sonic pi, so I’m scrambling up multiple learning curves at once.
That said, it’s made more difficult by having to guess at what ruby features were & weren’t included in the sonic pi dialect. And given that rings is (afaik) specific to sonic pi, and such a core feature, it’s ghastly that the docs for its use are so sketchy.
Don’t get me wrong, I’m loving sonic pi. But I’m not loving the snipe hunt.

So I figured out how to get ruby to list all methods for a class. Here are all the methods for the VectorRing class. But I don’t have info on what the args are, so you’ll need some trial and error.
One of note is the “vec” method, which turns a ring into a standard ruby array. Once it’s an array, you can use all the usual ruby methods to search, filter, etc.
Here’s the full list:

:___sp_vector_name,

:map_index,

:drop,

:last,

:<=>,

:uniq,

:compact,

:==,

:vec,

:,

:drop_last,

:choose,

:__sp_make_thread_safe,

:list_diff,

:empty?,

:eql?,

:scale,

:list_concat,

:ramp,

:reflect,

:mirror,

:repeat,

:take_last,

:butlast,

:pick,

:index,

:stretch,

:&,

:*,

:+,

:min,

:-,

:inspect,

:max,

:length,

:size,

:each,

:sp_thread_safe?,

:reverse,

:to_ary,

:to_a,

:join,

:rotate,

:to_s,

:sort,

:values_at,

:filter,

:flatten,

:map,

:flat_map,

:first,

:all?,

:any?,

:sample,

:shuffle,

:notes,

:each_with_index,

:ring,

:take,

:look,

:tick,

:present?,

:presence,

:sp_log_inspect,

:metaclass,

:meta_eval,

:meta_def,

:class_def,

:blank?,

:to_yaml,

:to_json,

:os,

:raspberry_pi?,

:raspberry_pi_2?,

:raspberry_pi_3?,

:raspberry_pi_4?,

:raspberry_pi_5?,

:num_buffers_for_current_os,

:num_audio_busses_for_current_os,

:unify_tilde_dir,

:default_sched_ahead_time,

:default_control_delta,

:global_uuid,

:__system_thread_locals,

:__exe_fix,

:fetch_url,

:log_raw,

:log_exception,

:__thread_locals,

:ensure_dir,

:log_info,

:osc_debug_mode,

:debug_mode,

:incoming_osc_debug_mode,

:resolve_synth_opts_hash_or_array,

:truthy?,

:zipmap,

:merge_synth_arg_maps_array,

:split_params_and_merge_opts_array,

:purge_nil_vals!,

:pp_el_or_list,

:log,

:arg_h_pp,

:safe_mode?,

:is_list_like?,

:__thread_locals_reset!,

:host_platform_desc,

:__system_thread_locals_reset!,

:__no_kill_block,

:singleton_class,

:dup,

:itself,

:methods,

:singleton_methods,

:protected_methods,

:private_methods,

:public_methods,

:instance_variables,

:instance_variable_get,

:instance_variable_set,

:instance_variable_defined?,

:remove_instance_variable,

:instance_of?,

:kind_of?,

:is_a?,

:display,

:public_send,

:extend,

:class,

:frozen?,

:tap,

:then,

:yield_self,

:clone,

:===,

:!~,

:method,

:public_method,

:singleton_method,

:nil?,

:respond_to?,

:define_singleton_method,

:hash,

:freeze,

:object_id,

:send,

:to_enum,

:enum_for,

:equal?,

:!,

:send,

:!=,

:id,

:instance_eval,

:instance_exec]

1 Like

Okay, I’ve done quite a bit of testing. Not every ring method is an array method, or vice versa. Some of the methods I unearthed baffled me, but here’s a useful working subset of ring methods.
Enjoy!
Harry

 <=> Compare 2 arrays. See Array.<=> in ruby docs for details. 
 & – set intersection
 * – set multiply. Equivalent to repeat n times. (ring 1, 2, 3) * 2 returns (ring 1, 2, 3, 1, 2, 3)
 + – add rings/lists together. (ring 1, 2, 3) + [4, 5] returns (ring 1, 2, 3, 4, 5)
 - – subtract one ring from another
 == – checks if 2 arrays are equal. 
 [] – specify an item or range of items
 all? – returns bool based on all items passing test in block.  (ring 1, 2, 3).all? |x| {x % 2 == 0} returns false
 any? – returns bool based on any items passing test in block. (ring 1, 2, 3).all? |x| {x % 2 == 0}returns false
 butlast – returns all but last item
 choose – pick an item at random
 compact – removes nils
 drop – deletes first item in array. Return value is the dropped item, not the ring. 
 drop_last – drop last item in list
 each – iterate through each member of the ring. 
 each_with_index – like each, but also returns index. (ring "a", "b", "c").each_with_index do |item, i|  puts "item " + item.to_s  puts "i " + i.to_s end
 empty? – returns boolean saying whether the ring ie smpty
 eql? – returns boolean if ring matches another ring
 filter – filter the array based on the condition in the specified block. 
 first – returns first element
 flat_map – ???
 flatten – flattens any nested rings/arrays into one level of array.
 index – same as indexing via []
 inspect – same as to_s
 join, – concatenates items in ring into a string. (ring 1, 2, 3).join returns “123”
 last – the last item in the ring.
 length – the number of elements in the ring. 
 list_concat – appends a list to the end of the ring
 list_diff – works like Array.difference
 look – read next value without ticking
 map – runs block, returns new ring with resulting values. (ring 3, 1, 2).map {|x| x * 2} returns (ring 6, 2, 4)
 max – the maximum elements. See min. 
 min – the minimum element(s). You can specify a number, but it defaults to 1. (ring 20, 5, 50, 10).min(2) returns (ring 5, 10)
 mirror – appends a mirror image of the elements, repeating the middle element. (ring 1, 2, 3).mirror returns (ring 1, 2, 3, 3, 2, 1)
 notes – same as to_a
 pick – returns random item
 ramp – returns a ring that's a ramp. I think this means that indices out of bounds just return the first (for negs) or last (for too high) values, instead of wrapping like a ring. 
 reflect – appends a mirror image of the elements, not repeating the middle element  (ring 1, 2, 3).reflect returns (ring 1, 2, 3, 2, 1)
 repeat – appends an exact copy to the end of the ring. (ring 1, 2, 3). repeat returns (ring 1, 2, 3, 1, 2, 3)
 reverse – returns a ring in reverse order
 ring – returns a ring. Also works on lists.  [“a”, “b”, “c”].ring returns (ring “a”, “b”, “c”)
 rotate – moves the first item to end of list. (ring 1, 2, 3).rotate returns (ring 2, 3, 1)
 sample – same as pick
 scale – returns a new ring with all elements multiplied by 2 (assumes ring contains numbers only) 
 shuffle – randomly rearranges elements
 size – same as length
 sort – sorts
 stretch – like repeat, but repeats each element. (ring 1, 2, 3).stretch returns (ring 1, 1, 2, 2, 3, 3)
 take – same as delete_at
 take_last – same as drop_last
 tick – used to iterate through lists 
 to_a – seems to do the same thing as to_ary
 to_ary – converts ring to an array. (ring 1, 2, 3).to_ary returns [1, 2, 3]. Is editable
 to_s – converts to string
 uniq – eliminate duplicates
 values_at – returns a new list of items at the specified indices. (ring 1, 2, 3).values_at 0, 2 returns (ring 1, 3)
 vec – returns a frozen, uneditable list. Better to use .list



1 Like

Hello,
Look for methods such as insertion, removal, access, and modification for lists, and circular traversal, insertion/removal, and access for rings. Explore community forums and tutorials for practical examples and guidance on utilizing these methods effectively in your projects.

Here’s a list I came up with:

  • subtract one ring from another
    & set intersection
  • set multiply. Equivalent to repeat n times. (ring 1, 2, 3) * 2 returns (ring 1, 2, 3, 1, 2, 3)
  • add rings/lists together. (ring 1, 2, 3) + [4, 5] returns (ring 1, 2, 3, 4, 5)
    <=> Compare 2 arrays. See Array.<=> in ruby docs for details.
    == checks if 2 arrays are equal.
    specify an item or range of items
    all? returns bool based on all items passing test in block. (ring 1, 2, 3).all? x {x % 2 == 0} returns false
    any? returns bool based on any items passing test in block. (ring 1, 2, 3).all? x {x % 2 == 0}returns false
    butlast returns all but last item
    choose pick an item at random
    compact removes nils
    drop deletes first item in array. Return value is the dropped item, not the ring.
    drop_last drop last item in list
    each iterate through each member of the ring.
    each_with_index like each, but also returns index. (ring “a”, “b”, “c”).each_with_index do item, i puts "item " + item.to_s puts "i " + i.to_s end
    empty? returns boolean saying whether the ring ie smpty
    eql? returns boolean if ring matches another ring
    filter filter the array based on the condition in the specified block.
    first returns first element
    flat_map ???
    flatten flattens any nested rings/arrays into one level of array.
    index same as indexing via
    join concatenates items in ring into a string. (ring 1, 2, 3).join returns “123”
    last the last item in the ring.
    list_concat appends a list to the end of the ring
    list_diff works like Array.difference
    look read next value without ticking
    map runs block, returns new ring with resulting values. (ring 3, 1, 2).map { x x * 2} returns (ring 6, 2, 4)
    max maximum element(s). see min.
    min the minimum element(s). You can specify a number, but it defaults to 1. (ring 20, 5, 50, 10).min(2) returns (ring 5, 10)
    mirror appends a mirror image of the elements, repeating the middle element. (ring 1, 2, 3).mirror returns (ring 1, 2, 3, 3, 2, 1)
    notes same as to_a
    pick returns random item
    ramp returns a ring that’s a ramp. I think this means that indices out of bounds just return the first (for negs) or last (for too high) values, instead of wrapping like a ring.
    reflect appends a mirror image of the elements, not repeating the middle element (ring 1, 2, 3).reflect returns (ring 1, 2, 3, 2, 1)
    repeat appends an exact copy to the end of the ring. (ring 1, 2, 3). repeat returns (ring 1, 2, 3, 1, 2, 3)
    reverse returns a ring in reverse order
    ring returns a ring. Also works on lists. [“a”, “b”, “c”].ring returns (ring “a”, “b”, “c”)
    rotate moves the first item to end of list. (ring 1, 2, 3).rotate returns (ring 2, 3, 1)
    sample same as pick
    scale returns a new ring with all elements multiplied by 2 (assumes ring contains numbers only)
    shuffle randomly rearranges elements
    size same as length
    sort sorts
    stretch like repeat, but repeats each element. (ring 1, 2, 3).stretch returns (ring 1, 1, 2, 2, 3, 3)
    take same as delete_at
    take_last same as drop_last
    tick used to iterate through lists
    to_ary converts ring to an array. (ring 1, 2, 3).to_ary returns [1, 2, 3]. Is editable
    to_a seems to do the same thing as to_ary
    to_s converts to string
    uniq eliminate duplicates
    values_at returns a new list of items at the specified indices. (ring 1, 2, 3).values_at 0, 2 returns (ring 1, 3)
    vec returns a frozen, uneditable list. Better to use .list