Use slice! first slice! to extract the part you want to remove:
a = [1, 2, 3, 4] ret = a.slice!(2,2)
This leaves [1,2] in a and [3,4] in ret . Then a simple []= insert new values:
a[2,0] = [:pancakes]
The result of [3,4] in ret and [1, 2, :pancakes] in a . Summarizing:
def splice(a, start, len, replacements = nil) r = a.slice!(start, len) a[start, 0] = replacements if(replacements) r end
You can also use *replacements if you want variable behavior:
def splice(a, start, len, *replacements) r = a.slice!(start, len) a[start, 0] = replacements if(replacements) r end
Patching a monkey and figuring out what you want to do from outside using start and len remains as an exercise.
source share