# Use Ruby Array Methods in Sonic PI

**URL:** <https://in-thread.sonic-pi.net/t/use-ruby-array-methods-in-sonic-pi/1749>\
**Category:** Creations & Ideas\
**Created:** [November 29, 2018, 7:11pm UTC](https://in-thread.sonic-pi.net/t/use-ruby-array-methods-in-sonic-pi/1749 "2018-11-29T19:11:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoeMac](https://avatars.discourse-cdn.com/v4/letter/j/48db29/32.png) [@JoeMac](https://in-thread.sonic-pi.net/u/JoeMac)\
**Post date:** [November 29, 2018, 7:11pm UTC](https://in-thread.sonic-pi.net/t/use-ruby-array-methods-in-sonic-pi/1749/1 "2018-11-29T19:11:32Z")

</div>

#29Nov2018

```ruby
#Using Ruby array methods with SonicPI
# Google ruby array methods
# not every Ruby method is implemented in Sonic PI
# https://ruby-doc.org/core-2.2.0/Array.html

use_synth :piano
rel= 1.5 # note release
nle=[0.125] # note length
nle=nle.ring
amplitude= 1
key= 'C4'
scaletype='major'
cscale=knit(key,16)

with_fx :level, amp: amplitude do
  play_pattern_timed cscale.zip(scale(key,scaletype)+scale(key,scaletype).
                                reverse).flatten,nle,release: nle[0]*rel
end

with_fx :level, amp: amplitude do
  play_pattern_timed cscale.zip(scale(key,scaletype)+scale(key,scaletype).
                                reverse),nle,release: nle[0]*rel
end

```

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [November 29, 2018, 7:47pm UTC](https://in-thread.sonic-pi.net/t/use-ruby-array-methods-in-sonic-pi/1749/2 "2018-11-29T19:47:12Z")

</div>

I’ve taken the liberty of editing your post and adding ````` before and after the code.  
As you had it before, just pasting the code in directly, the single quotes around C4 had been converted to smart quotes.  
If you precede and follow your code with three reverse ticks then it posts correctly. It also has the bonus of  
added colour to show the syntax.  
PS it sounds nice 😀

---

<div class="post-metadata">

**Author:** ![jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jonny](https://in-thread.sonic-pi.net/u/jonny)\
**Post date:** [September 17, 2023, 2:38pm UTC](https://in-thread.sonic-pi.net/t/use-ruby-array-methods-in-sonic-pi/1749/3 "2023-09-17T14:38:25Z")

</div>

**undefined method `zip’**

```ruby
Runtime Error: [buffer 2, line 15] - NoMethodError

Thread death!

undefined method `zip' for (ring "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4", "C4"):SonicPi::Core::RingVector

```

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [September 17, 2023, 8:36pm UTC](https://in-thread.sonic-pi.net/t/use-ruby-array-methods-in-sonic-pi/1749/4 "2023-09-17T20:36:38Z")

</div>

> [@jonny](#):
>
> undefined method `zip’

This is a good example of having to be careful with some ruby methods with Sonic Pi. The problem is that cscale defined as `knit(key.16)` has to be converted to an array list using .to\_a if you want to user .zip with it. So change the program as below and it works.

```ruby
#Using Ruby array methods with SonicPI
# Google ruby array methods
# not every Ruby method is implemented in Sonic PI
# https://ruby-doc.org/core-2.2.0/Array.html

use_synth :piano
rel= 1.5 # note release
nle=[0.125] # note length
nle=nle.ring
amplitude= 1
key= 'C4'
scaletype='major'
cscale=knit(key,16).to_a

with_fx :level, amp: amplitude do
  play_pattern_timed cscale.zip(scale(key,scaletype)+scale(key,scaletype).
                                reverse).flatten,nle,release: nle[0]*rel
end

with_fx :level, amp: amplitude do
  play_pattern_timed cscale.zip(scale(key,scaletype)+scale(key,scaletype).
                                reverse),nle,release: nle[0]*rel
end

```

It shows that you have to be careful using ruby constructs and sometimes be prepared to play around with them to get them to work in Sonic PI EDIT I think this worked as per the orginal with earlier versions of SP several years ago.
