|
159 | 159 | # - #rstrip, #rstrip!: Strip trailing whitespace.
|
160 | 160 | # - #strip, #strip!: Strip leading and trailing whitespace.
|
161 | 161 | #
|
162 |
| -# == +String+ Slices |
163 |
| -# |
164 |
| -# A _slice_ of a string is a substring selected by certain criteria. |
165 |
| -# |
166 |
| -# These instance methods utilize slicing: |
167 |
| -# |
168 |
| -# - String#[] (aliased as String#slice): Returns a slice copied from +self+. |
169 |
| -# - String#[]=: Mutates +self+ with the slice replaced. |
170 |
| -# - String#slice!: Mutates +self+ with the slice removed and returns the removed slice. |
171 |
| -# |
172 |
| -# Each of the above methods takes arguments that determine the slice |
173 |
| -# to be copied or replaced. |
174 |
| -# |
175 |
| -# The arguments have several forms. |
176 |
| -# For a string +string+, the forms are: |
177 |
| -# |
178 |
| -# - <tt>string[index]</tt> |
179 |
| -# - <tt>string[start, length]</tt> |
180 |
| -# - <tt>string[range]</tt> |
181 |
| -# - <tt>string[regexp, capture = 0]</tt> |
182 |
| -# - <tt>string[substring]</tt> |
183 |
| -# |
184 |
| -# <b><tt>string[index]</tt></b> |
185 |
| -# |
186 |
| -# When a non-negative integer argument +index+ is given, |
187 |
| -# the slice is the 1-character substring found in +self+ at character offset +index+: |
188 |
| -# |
189 |
| -# 'bar'[0] # => "b" |
190 |
| -# 'bar'[2] # => "r" |
191 |
| -# 'bar'[20] # => nil |
192 |
| -# 'тест'[2] # => "с" |
193 |
| -# 'こんにちは'[4] # => "は" |
194 |
| -# |
195 |
| -# When a negative integer +index+ is given, |
196 |
| -# the slice begins at the offset given by counting backward from the end of +self+: |
197 |
| -# |
198 |
| -# 'bar'[-3] # => "b" |
199 |
| -# 'bar'[-1] # => "r" |
200 |
| -# 'bar'[-20] # => nil |
201 |
| -# |
202 |
| -# <b><tt>string[start, length]</tt></b> |
203 |
| -# |
204 |
| -# When non-negative integer arguments +start+ and +length+ are given, |
205 |
| -# the slice begins at character offset +start+, if it exists, |
206 |
| -# and continues for +length+ characters, if available: |
207 |
| -# |
208 |
| -# 'foo'[0, 2] # => "fo" |
209 |
| -# 'тест'[1, 2] # => "ес" |
210 |
| -# 'こんにちは'[2, 2] # => "にち" |
211 |
| -# # Zero length. |
212 |
| -# 'foo'[2, 0] # => "" |
213 |
| -# # Length not entirely available. |
214 |
| -# 'foo'[1, 200] # => "oo" |
215 |
| -# # Start out of range. |
216 |
| -# 'foo'[4, 2] # => nil |
217 |
| -# |
218 |
| -# Special case: if +start+ equals the length of +self+, |
219 |
| -# the slice is a new empty string: |
220 |
| -# |
221 |
| -# 'foo'[3, 2] # => "" |
222 |
| -# 'foo'[3, 200] # => "" |
223 |
| -# |
224 |
| -# When a negative +start+ and non-negative +length+ are given, |
225 |
| -# the slice begins by counting backward from the end of +self+, |
226 |
| -# and continues for +length+ characters, if available: |
227 |
| -# |
228 |
| -# 'foo'[-2, 2] # => "oo" |
229 |
| -# 'foo'[-2, 200] # => "oo" |
230 |
| -# # Start out of range. |
231 |
| -# 'foo'[-4, 2] # => nil |
232 |
| -# |
233 |
| -# When a negative +length+ is given, there is no slice: |
234 |
| -# |
235 |
| -# 'foo'[1, -1] # => nil |
236 |
| -# 'foo'[-2, -1] # => nil |
237 |
| -# |
238 |
| -# <b><tt>string[range]</tt></b> |
239 |
| -# |
240 |
| -# When a Range argument +range+ is given, |
241 |
| -# it creates a substring of +string+ using the indices in +range+. |
242 |
| -# The slice is then determined as above: |
243 |
| -# |
244 |
| -# 'foo'[0..1] # => "fo" |
245 |
| -# 'foo'[0, 2] # => "fo" |
246 |
| -# |
247 |
| -# 'foo'[2...2] # => "" |
248 |
| -# 'foo'[2, 0] # => "" |
249 |
| -# |
250 |
| -# 'foo'[1..200] # => "oo" |
251 |
| -# 'foo'[1, 200] # => "oo" |
252 |
| -# |
253 |
| -# 'foo'[4..5] # => nil |
254 |
| -# 'foo'[4, 2] # => nil |
255 |
| -# |
256 |
| -# 'foo'[-4..-3] # => nil |
257 |
| -# 'foo'[-4, 2] # => nil |
258 |
| -# |
259 |
| -# 'foo'[3..4] # => "" |
260 |
| -# 'foo'[3, 2] # => "" |
261 |
| -# |
262 |
| -# 'foo'[-2..-1] # => "oo" |
263 |
| -# 'foo'[-2, 2] # => "oo" |
264 |
| -# |
265 |
| -# 'foo'[-2..197] # => "oo" |
266 |
| -# 'foo'[-2, 200] # => "oo" |
267 |
| -# |
268 |
| -# <b><tt>string[regexp, capture = 0]</tt></b> |
269 |
| -# |
270 |
| -# When the Regexp argument +regexp+ is given, |
271 |
| -# and the +capture+ argument is <tt>0</tt>, |
272 |
| -# the slice is the first matching substring found in +self+: |
273 |
| -# |
274 |
| -# 'foo'[/o/] # => "o" |
275 |
| -# 'foo'[/x/] # => nil |
276 |
| -# s = 'hello there' |
277 |
| -# s[/[aeiou](.)\1/] # => "ell" |
278 |
| -# s[/[aeiou](.)\1/, 0] # => "ell" |
279 |
| -# |
280 |
| -# If the argument +capture+ is provided and not <tt>0</tt>, |
281 |
| -# it should be either a capture group index (integer) |
282 |
| -# or a capture group name (String or Symbol); |
283 |
| -# the slice is the specified capture |
284 |
| -# (see {Groups and Captures}[rdoc-ref:Regexp@Groups+and+Captures]): |
285 |
| -# |
286 |
| -# s = 'hello there' |
287 |
| -# s[/[aeiou](.)\1/, 1] # => "l" |
288 |
| -# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l" |
289 |
| -# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e" |
290 |
| -# |
291 |
| -# If an invalid capture group index is given, there is no slice. |
292 |
| -# If an invalid capture group name is given, +IndexError+ is raised. |
293 |
| -# |
294 |
| -# <b><tt>string[substring]</tt></b> |
295 |
| -# |
296 |
| -# When the single +String+ argument +substring+ is given, |
297 |
| -# it returns the substring from +self+ if found, otherwise +nil+: |
298 |
| -# |
299 |
| -# 'foo'['oo'] # => "oo" |
300 |
| -# 'foo'['xx'] # => nil |
301 |
| -# |
302 | 162 | # == What's Here
|
303 | 163 | #
|
304 | 164 | # First, what's elsewhere. Class +String+:
|
|
0 commit comments