summaryrefslogtreecommitdiff
path: root/doc/string/aset.rdoc
blob: db9079ebfb81888abec387a6b935f0dbc3ec47ec (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
Returns +self+ with all, a substring, or none of its contents replaced;
returns the argument +other_string+.

<b>Form <tt>self[index] = other_string</tt></b>

With non-negative integer argument +index+ given,
searches for the 1-character substring found in self at character offset index:

  s = 'hello'
  s[0] = 'foo' # => "foo"
  s            # => "fooello"

  s = 'hello'
  s[4] = 'foo' # => "foo"
  s            # => "hellfoo"

  s = 'hello'
  s[5] = 'foo' # => "foo"
  s            # => "hellofoo"

  s = 'hello'
  s[6] = 'foo' # Raises IndexError: index 6 out of string.

With negative integer argument +index+ given,
counts backward from the end of +self+:

  s = 'hello'
  s[-1] = 'foo'  # => "foo"
  s              # => "hellfoo"

  s = 'hello'
  s[-5] = 'foo'  # => "foo"
  s              # => "fooello"

  s = 'hello'
  s[-6] = 'foo'  # Raises IndexError: index -6 out of string.

<b>Form <tt>self[start, length] = other_string</tt></b>

With integer arguments +start+ and +length+ given,
searches for a substring of size +length+ characters (as available)
beginning at character offset specified by +start+.

If argument +start+ is non-negative,
the offset is +start+:

  s = 'hello'
  s[0, 1] = 'foo'  # => "foo"
  s                # => "fooello"

  s = 'hello'
  s[0, 5] = 'foo'  # => "foo"
  s                # => "foo"

  s = 'hello'
  s[0, 9] = 'foo'  # => "foo"
  s                # => "foo"

  s = 'hello'
  s[2, 0] = 'foo'  # => "foo"
  s                # => "hefoollo"

  s = 'hello'
  s[2, -1] = 'foo' # Raises IndexError: negative length -1.

If argument +start+ is negative,
counts backward from the end of +self+:

  s = 'hello'
  s[-1, 1] = 'foo' # => "foo"
  s                # => "hellfoo"

  s = 'hello'
  s[-1, 9] = 'foo' # => "foo"
  s                # => "hellfoo"

  s = 'hello'
  s[-5, 2] = 'foo' # => "foo"
  s                # => "foollo"

  s = 'hello'
  s[-3, 0] = 'foo' # => "foo"
  s                # => "hefoollo"

  s = 'hello'
  s[-6, 2] = 'foo' # Raises IndexError: index -6 out of string.

Special case: if +start+ equals the length of +self+,
the argument is appended to +self+:

  s = 'hello'
  s[5, 3] = 'foo' # => "foo"
  s               # => "hellofoo"

<b>Form <tt>self[range] = other_string</tt></b>

With Range argument +range+ given,
equivalent to <tt>self[range.start, range.size] = other_string</tt>:

  s0 = 'hello'
  s1 = 'hello'
  s0[0..2] = 'foo' # => "foo"
  s1[0, 3] = 'foo' # => "foo"
  s0               # => "foolo"
  s1               # => "foolo"

  s = 'hello'
  s[0...2] = 'foo' # => "foo"
  s                # => "foollo"

  s = 'hello'
  s[0...0] = 'foo' # => "foo"
  s                # => "foohello"

  s = 'hello'
  s[9..10] = 'foo' # Raises RangeError: 9..10 out of range

<b>Form <tt>self[regexp, capture = 0] = other_string</tt></b>

With Regexp argument +regexp+ given and +capture+ as zero,
searches for a matching substring in +self+;
updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables]:

  s = 'hello'
  s[/l/] = 'L'       # => "L"
  [$`, $&, $']       # => ["he", "l", "lo"]
  s[/eLlo/] = 'owdy' # => "owdy"
  [$`, $&, $']       # => ["h", "eLlo", ""]
  s[/eLlo/] = 'owdy' # Raises IndexError: regexp not matched.
  [$`, $&, $']       # => [nil, nil, nil]

With +capture+ as a positive integer +n+,
searches for the +n+th matched group:

  s = 'hello'
  s[/(h)(e)(l+)(o)/] = 'foo'    # => "foo"
  [$`, $&, $']                  # => ["", "hello", ""]

  s = 'hello'
  s[/(h)(e)(l+)(o)/, 1] = 'foo' # => "foo"
  s                             # => "fooello"
  [$`, $&, $']                  # => ["", "hello", ""]

  s = 'hello'
  s[/(h)(e)(l+)(o)/, 2] = 'foo' # => "foo"
  s                             # => "hfoollo"
  [$`, $&, $']                  # => ["", "hello", ""]

  s = 'hello'
  s[/(h)(e)(l+)(o)/, 4] = 'foo' # => "foo"
  s                             # => "hellfoo"
  [$`, $&, $']                  # => ["", "hello", ""]

  s = 'hello'
  # => "hello"
  s[/(h)(e)(l+)(o)/, 5] = 'foo  # Raises IndexError: index 5 out of regexp.

  s = 'hello'
  s[/nosuch/] = 'foo'           # Raises IndexError: regexp not matched.

<b>Form <tt>self[substring] = other_string</tt></b>

With string argument +substring+ given:

  s = 'hello'
  s['l'] = 'foo'  # => "foo"
  s  # => "hefoolo"

  s = 'hello'
  s['ll'] = 'foo'  # => "foo"
  s  # => "hefooo"

  s = 'Привет'
  s['ив'] = 'foo'  # => "foo"
  s  # => "Прfooет"

  s = 'こんにちは'
  s['んにち'] = 'foo'  # => "foo"
  s  # => "こfooは"

  s['nosuch'] = 'foo' # Raises IndexError: string not matched.

Related: see {Modifying}[rdoc-ref:String@Modifying].