开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜