Ruby Method of the Day - Array.slice!

Posted by Kelly McCauley on Oct 31, 2007

Signature

array.slice!(position)          #=> object or nil

array.slice!(position) removes the object at position from array and returns the removed object. If position is outside of array then nil is returned. If position is 0 or positive then start counting from the beginning of the array. If position is negative then start counting from the end of the array.

array.slice!(position, count)   #=> new_array or nil

array.slice!(position, count) removes count number of objects, starting at position, from array and returns new_array containing the objects that were removed from the array. If position is outside of array then nil is returned. count must be either 0 or a positive integer or an IndexError is thrown. If position is 0 or positive then start counting from the beginning of the array. If position is negative then start counting from the end of the array.

array.slice!(range)             #=> new_array or nil

array.slice!(range) removes all objects at the positions specified by range from array and returns new_array containing the objects that were removed from the array. nil is returned if the start of the range is positive and outside of the array. RangeError is thrown if the start of the range is negative and outside of the array. If the start of the range is 0 or positive then start counting from the beginning of the array. If the start of the range is negative then start counting from the end of the array.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
a = [1, 2, "foo", 3, "bar", 4, -9]
b = a.clone; b.slice!(0)      #=> 1
b                             #=> [2, "foo", 3, "bar", 4, -9]
b = a.clone; b.slice!(-1)     #=> -9
b                             #=> [1, 2, "foo", 3, "bar", 4]
b = a.clone; b.slice!(20)     #=> nil
b                             #=> [1, 2, "foo", 3, "bar", 4, -9]
b = a.clone; b.slice!(-20)    #=> nil
b                             #=> [1, 2, "foo", 3, "bar", 4, -9]


b = a.clone; b.slice!(1,3)    #=> [2, "foo", 3]
b                             #=> [1, "bar", 4, -9]

begin
  b = a.clone; b.slice!(1,-3)
rescue Exception => e
  e.inspect                   #=> "#<IndexError: negative length (-3)>"
end

b = a.clone; b.slice!(-2,3)   #=> [4, -9]
b                             #=> [1, 2, "foo", 3, "bar"]
b = a.clone; b.slice!(-3,3)   #=> ["bar", 4, -9]
b                             #=> [1, 2, "foo", 3]
b = a.clone; b.slice!(-8,2)   #=> nil
b                             #=> [1, 2, "foo", 3, "bar", 4]


b = a.clone; b.slice!(3..5)   #=> [3, "bar", 4]
b                             #=> [1, 2, "foo", -9]
b = a.clone; b.slice!(0..99)  #=> [1, 2, "foo", 3, "bar", 4, -9]
b                             #=> []
b = a.clone; b.slice!(2..5)   #=> ["foo", 3, "bar", 4]
b                             #=> [1, 2, -9]
b = a.clone; b.slice!(2..-4)  #=> ["foo", 3]
b                             #=> [1, 2, "bar", 4, -9]
b = a.clone; b.slice!(2..-99) #=> []
b                             #=> [1, 2, "foo", 3, "bar", 4, -9]
b = a.clone; b.slice!(-5..-2) #=> ["foo", 3, "bar", 4]
b                             #=> [1, 2, -9]

begin
  b = a.clone; b.slice!(-99..-1)
rescue Exception => e
  e.inspect                   #=> "#<RangeError: -99..-1 out of range>"
end

Documentation Reference

Ruby version 1.8.6

www.ruby-doc.org : Array.slice!