Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.prototype.splice in Ruby

A friend asked me the Ruby best and performant way to achieve the effect of JavaScript's splice method in Ruby. This means no iteration on the Array itself or copies.

"begin at index start, remove length items and (optionally) insert elements. Finally return the removed items in an array." << This is misleading, see the JS example below.

http://www.mennovanslooten.nl/blog/post/41

Quick hack that doesn't have the optional substitution:

from_index     = 2
for_elements   = 2
sostitute_with = :test
initial_array  = [:a, :c, :h, :g, :t, :m]
# expected result: [:a, :c, :test, :t, :m]
initial_array[0..from_index-1] + [sostitute_with] + initial_array[from_index + for_elements..-1]

What's yours? One line is better.

Update:

// JavaScript
var a = ['a', 'c', 'h', 'g', 't', 'm'];
var b = a.splice(2, 2, 'test'); 
> b is now ["h", "g"]
> a is now ["a", "c", "test", "t", "m"]

I need the resulting 'a' Array, not 'b'.

like image 946
kain Avatar asked Jul 31 '11 21:07

kain


2 Answers

Use Array#[]=.

a = [1, 2, 3, 4, 5, 6]
a[2..4] = [:foo, :bar, :baz, :wibble]
a # => [1, 2, :foo, :bar, :baz, :wibble, 6]

# It also supports start/length instead of a range:
a[0, 3] = [:a, :b]
a # => [:a, :b, :bar, :baz, :wibble, 6]

As for returning the removed elements, []= doesn't do that... You could write your own helper method to do it:

class Array
  def splice(start, len, *replace)
    ret = self[start, len]
    self[start, len] = replace
    ret
  end
end
like image 126
jtbandes Avatar answered Oct 09 '22 14:10

jtbandes


First use slice! to extract the part you want to delete:

a   = [1, 2, 3, 4]
ret = a.slice!(2,2)

That leaves [1,2] in a and [3,4] in ret. Then a simple []= to insert the new values:

a[2,0] = [:pancakes]

The result is [3,4] in ret and [1, 2, :pancakes] in a. Generalizing:

def splice(a, start, len, replacements = nil)
    r = a.slice!(start, len)
    a[start, 0] = replacements if(replacements)
    r
end

You could also use *replacements if you want variadic behavior:

def splice(a, start, len, *replacements)
    r = a.slice!(start, len)
    a[start, 0] = replacements if(replacements)
    r
end
like image 3
mu is too short Avatar answered Oct 09 '22 15:10

mu is too short