简体   繁体   中英

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'.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM