Ruby Method of the Day - Array.[]=

Posted by Kelly McCauley on Oct 30, 2007

Signature

Array.[]= assigns/replaces elements in an array and is the counter method to Array.[].

array[position] = object              #=> object

array[position] = object replaces the object at position with object and returns object. If position is negative and outside of array, then an IndexError exception is thrown. If position is positive and outside of array, then array is padded with (position - array.length) number of nil objects and object is placed at position.

array[position, count] = object       #=> object
array[position, count] = other_array  #=> other_array

array[position, count] = object replaces count number of objects, starting at position, with object. Similarly, array[position, count] = other_array replaces count number of objects, starting at position, with all of the objects in other_array. count must be either 0 or a positive integer or an IndexError is thrown. If position is negative and outside of array, then an IndexError exception is thrown. If position is positive and outside of array, then array is padded with (position - array.length) number of nil objects and object is placed at position.

array[range] = object                 #=> object
array[range] = other_array            #=> other_array

array[range] = object replaces all objects at the positions specified by range with object. Similarly, array[range] = other_array replaces all objects at the positions specified by range with all of the objects in other_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 string. RangeError is thrown if the start of the range is negative and is outside 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
a = [1, 2, "foo", 3, "bar", 4, -9]

b = a.clone; b[0] = nil; b        #=> [nil, 2, "foo", 3, "bar", 4, -9]
b = a.clone; b[0] = [5,4,3,2]; b  #=> [[5, 4, 3, 2], 2, "foo", 3, "bar", 4, -9]
b = a.clone; b[1] = nil; b        #=> [1, nil, "foo", 3, "bar", 4, -9]
b = a.clone; b[-1] = nil; b       #=> [1, 2, "foo", 3, "bar", 4, nil]

b = a.clone
b.length                          #=> 7
# Scroll to right! ===>
b[20] = nil; b                    #=> [1, 2, "foo", 3, "bar", 4, -9, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
b.length                          #=> 21

begin
  b = a.clone
  b[-20] = nil
rescue Exception => e
  e.inspect                         #=> "#<IndexError: index -20 out of array>"
end

b = a.clone; b[2,3] = 'xxxx'; b     #=> [1, 2, "xxxx", 4, -9]
b = a.clone; b[2,3] = %w{x x x}; b  #=> [1, 2, "x", "x", "x", 4, -9]
b = a.clone; b[2,4] = [5,4,3,2]; b  #=> [1, 2, 5, 4, 3, 2, -9]
b = a.clone; b[2,4] = [5,4,3,2,1,0,-1,-2,-3]; b   #=> [1, 2, 5, 4, 3, 2, 1, 0, -1, -2, -3, -9]

begin
  b = a.clone
  b[2,-3] = 'xxxx'
rescue Exception => e
  e.inspect                         #=> "#<IndexError: negative length (-3)>"
end

b = a.clone; b[7,2] = 'xxxx'; b     #=> [1, 2, "foo", 3, "bar", 4, -9, "xxxx"]
b = a.clone; b[1,0] = 'xxxx'; b     #=> [1, "xxxx", 2, "foo", 3, "bar", 4, -9]
b = a.clone; b[-2,3] = 'xxxx'; b    #=> [1, 2, "foo", 3, "bar", "xxxx"]
b = a.clone; b[-3,3] = 'xxxx'; b    #=> [1, 2, "foo", 3, "xxxx"]

begin
  b = a.clone
  b[-8,2] = 'xxxx'
rescue Exception => e
  e.inspect                         #=> "#<IndexError: index -8 out of array>"
end

# Scroll to right! ===>
b = a.clone; b[99,5] = 'xxxx'; b    #=> [1, 2, "foo", 3, "bar", 4, -9, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "xxxx"]
b.length                            #=> 100


b = a.clone; b[0..99] = 'xxxx'; b       #=> ["xxxx"]
b = a.clone; b[2..5] = 'xxxx'; b        #=> [1, 2, "xxxx", -9]
b = a.clone; b[2..5] = Array.new(4); b  #=> [1, 2, nil, nil, nil, nil, -9]
b = a.clone; b[2...5] = 'xxxx'; b       #=> [1, 2, "xxxx", 4, -9]
b = a.clone; b[2..-4] = 'xxxx'; b       #=> [1, 2, "xxxx", "bar", 4, -9]
b = a.clone; b[2..-99] = 'xxxx'; b      #=> [1, 2, "xxxx", "foo", 3, "bar", 4, -9]
b = a.clone; b[7..10] = 'xxxx'; b       #=> [1, 2, "foo", 3, "bar", 4, -9, "xxxx"]
b = a.clone; b[0..0] = 'xxxx'; b        #=> ["xxxx", 2, "foo", 3, "bar", 4, -9]
b = a.clone; b[-5..-2] = 'xxxx'; b      #=> [1, 2, "xxxx", -9]
b = a.clone; b[-5..-1] = 'xxxx'; b      #=> [1, 2, "xxxx"]
b = a.clone; b[-5..-6] = 'xxxx'; b      #=> [1, 2, "xxxx", "foo", 3, "bar", 4, -9]

begin
  b = a.clone
  b[-99..-1] = 'xxxx'
rescue Exception => e
  e.inspect                         #=> "#<RangeError: -99..-1 out of range>"
end


Documentation Reference

Ruby version 1.8.6

www.ruby-doc.org : Array.[]=