hash-utils 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.txt +8 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +2 -5
- data/LICENSE.txt +1 -1
- data/README.md +3 -3
- data/Rakefile +3 -4
- data/VERSION +1 -1
- data/hash-utils.gemspec +8 -8
- data/lib/hash-utils/array.rb +120 -92
- data/lib/hash-utils/boolean.rb +43 -21
- data/lib/hash-utils/file.rb +61 -43
- data/lib/hash-utils/gem.rb +16 -12
- data/lib/hash-utils/hash.rb +306 -264
- data/lib/hash-utils/io.rb +7 -3
- data/lib/hash-utils/module.rb +14 -10
- data/lib/hash-utils/nil.rb +7 -3
- data/lib/hash-utils/numeric.rb +26 -12
- data/lib/hash-utils/object.rb +165 -52
- data/lib/hash-utils/proc.rb +7 -3
- data/lib/hash-utils/string.rb +289 -173
- data/lib/hash-utils/stringio.rb +7 -3
- data/lib/hash-utils/symbol.rb +47 -24
- data/test +7 -1
- metadata +20 -22
data/lib/hash-utils/proc.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
require "hash-utils/object"
|
3
5
|
|
4
6
|
##
|
5
7
|
# Proc extension.
|
@@ -15,8 +17,10 @@ class Proc
|
|
15
17
|
# @since 0.18.0
|
16
18
|
#
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
+
if not self.__hash_utils_instance_respond_to? :proc?
|
21
|
+
def proc?
|
22
|
+
true
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
26
|
end
|
data/lib/hash-utils/string.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
# (c) 2011-2012 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
4
|
require "ruby-version"
|
5
5
|
require "hash-utils/array"
|
@@ -17,56 +17,72 @@ class String
|
|
17
17
|
# @since 0.3.0
|
18
18
|
#
|
19
19
|
|
20
|
-
|
20
|
+
if not self.constants.include? :NUMERIC
|
21
|
+
NUMERIC = /^\s*-?\d+(?:\.\d+)?\s*$/
|
22
|
+
end
|
21
23
|
|
22
24
|
##
|
23
25
|
# Holds character interlacing matcher.
|
24
26
|
# @since 0.18.1
|
25
27
|
#
|
26
28
|
|
27
|
-
|
29
|
+
if not self.constants.include? :INTERLACING
|
30
|
+
INTERLACING = /(.)([^$])/
|
31
|
+
end
|
28
32
|
|
29
33
|
##
|
30
34
|
# Holds lower case alpha characters for random string generator.
|
31
35
|
# @since 0.19.0
|
32
36
|
#
|
33
37
|
|
34
|
-
|
35
|
-
|
38
|
+
if not self.constants.include? :RANDOM_ALPHA_LOWER
|
39
|
+
RANDOM_ALPHA_LOWER = "abcdefghijklmnopqrstuvwxyz"
|
40
|
+
end
|
41
|
+
|
36
42
|
##
|
37
43
|
# Holds upper case alpha characters for random string generator.
|
38
44
|
# @since 0.19.0
|
39
45
|
#
|
40
46
|
|
41
|
-
|
47
|
+
if not self.constants.include? :RANDOM_ALPHA_UPPER
|
48
|
+
RANDOM_ALPHA_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
49
|
+
end
|
42
50
|
|
43
51
|
##
|
44
52
|
# Holds number characters list for random string generator.
|
45
53
|
# @since 0.19.0
|
46
54
|
#
|
47
55
|
|
48
|
-
|
56
|
+
if not self.constants.include? :RANDOM_NUMBERS
|
57
|
+
RANDOM_NUMBERS = "0123456789"
|
58
|
+
end
|
49
59
|
|
50
60
|
##
|
51
61
|
# Holds symbols list for random string generator.
|
52
62
|
# @since 0.19.0
|
53
63
|
#
|
54
64
|
|
55
|
-
|
65
|
+
if not self.constants.include? :RANDOM_SYMBOLS
|
66
|
+
RANDOM_SYMBOLS = "!\/#?.,_-+*@%&{}[]()=<>|~'$"
|
67
|
+
end
|
56
68
|
|
57
69
|
##
|
58
70
|
# Holds whitespace for random string generator.
|
59
71
|
# @since 0.19.0
|
60
72
|
#
|
61
73
|
|
62
|
-
|
74
|
+
if not self.constants.include? :RANDOM_WHITESPACE
|
75
|
+
RANDOM_WHITESPACE = " "
|
76
|
+
end
|
63
77
|
|
64
78
|
##
|
65
79
|
# Holds full spectrum of possible characters in random string generator.
|
66
80
|
# @since 0.19.0
|
67
81
|
#
|
68
82
|
|
69
|
-
|
83
|
+
if not self.constants.include? :RANDOM_FULL
|
84
|
+
RANDOM_FULL = self::RANDOM_ALPHA_LOWER + self::RANDOM_ALPHA_UPPER + self::RANDOM_NUMBERS + self::RANDOM_SYMBOLS + self::RANDOM_WHITESPACE
|
85
|
+
end
|
70
86
|
|
71
87
|
##
|
72
88
|
# Returns random string.
|
@@ -77,14 +93,16 @@ class String
|
|
77
93
|
# @since 0.19.0
|
78
94
|
#
|
79
95
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
96
|
+
if not self.respond_to? :random
|
97
|
+
def self.random(length, characters = self::RANDOM_FULL)
|
98
|
+
result = ""
|
99
|
+
max = characters.length
|
100
|
+
length.times do
|
101
|
+
result << characters[Kernel.rand(max)].chr
|
102
|
+
end
|
103
|
+
|
104
|
+
return result
|
85
105
|
end
|
86
|
-
|
87
|
-
return result
|
88
106
|
end
|
89
107
|
|
90
108
|
##
|
@@ -94,11 +112,13 @@ class String
|
|
94
112
|
# @since 0.3.0
|
95
113
|
#
|
96
114
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
115
|
+
if not self.__hash_utils_instance_respond_to? :numeric?
|
116
|
+
def numeric?
|
117
|
+
if self.match(self.class::NUMERIC)
|
118
|
+
true
|
119
|
+
else
|
120
|
+
false
|
121
|
+
end
|
102
122
|
end
|
103
123
|
end
|
104
124
|
|
@@ -130,13 +150,15 @@ class String
|
|
130
150
|
# @since 0.4.0
|
131
151
|
#
|
132
152
|
|
133
|
-
|
134
|
-
|
135
|
-
block
|
153
|
+
if not self.__hash_utils_instance_respond_to? :strtr
|
154
|
+
def strtr(defs, mode = nil, &block)
|
155
|
+
if block.nil?
|
156
|
+
block = Proc::new { |s| s }
|
157
|
+
end
|
158
|
+
|
159
|
+
defs, matcher = __prepare_strtr(defs, mode)
|
160
|
+
self.gsub(matcher) { |s| defs[block.call(s)] }
|
136
161
|
end
|
137
|
-
|
138
|
-
defs, matcher = __prepare_strtr(defs, mode)
|
139
|
-
self.gsub(matcher) { |s| defs[block.call(s)] }
|
140
162
|
end
|
141
163
|
|
142
164
|
##
|
@@ -151,13 +173,15 @@ class String
|
|
151
173
|
# @since 0.4.0
|
152
174
|
#
|
153
175
|
|
154
|
-
|
155
|
-
|
156
|
-
block
|
176
|
+
if not self.__hash_utils_instance_respond_to? :strtr!
|
177
|
+
def strtr!(defs, mode = nil, &block)
|
178
|
+
if block.nil?
|
179
|
+
block = Proc::new { |s| s }
|
180
|
+
end
|
181
|
+
|
182
|
+
defs, matcher = __prepare_strtr(defs, mode)
|
183
|
+
self.gsub!(matcher) { |s| defs[block.call(s)] }
|
157
184
|
end
|
158
|
-
|
159
|
-
defs, matcher = __prepare_strtr(defs, mode)
|
160
|
-
self.gsub!(matcher) { |s| defs[block.call(s)] }
|
161
185
|
end
|
162
186
|
|
163
187
|
##
|
@@ -167,12 +191,15 @@ class String
|
|
167
191
|
# foo = "012"
|
168
192
|
# puts foo.to_a.inspect # prints out ["0", "1", "2"]
|
169
193
|
#
|
194
|
+
# @param String glue glue according to convert to array
|
170
195
|
# @return [Array] array of single character strings
|
171
196
|
# @since 0.6.0
|
172
197
|
#
|
173
198
|
|
174
|
-
|
175
|
-
|
199
|
+
if not self.__hash_utils_instance_respond_to? :to_a
|
200
|
+
def to_a(glue = "")
|
201
|
+
self.split(glue)
|
202
|
+
end
|
176
203
|
end
|
177
204
|
|
178
205
|
##
|
@@ -187,13 +214,15 @@ class String
|
|
187
214
|
# @since 0.6.0
|
188
215
|
#
|
189
216
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
217
|
+
if not self.__hash_utils_instance_respond_to? :map
|
218
|
+
def map(&block)
|
219
|
+
buffer = " " * self.length
|
220
|
+
self.length.times do |i|
|
221
|
+
buffer[i] = block.call(self[i])
|
222
|
+
end
|
223
|
+
|
224
|
+
return buffer
|
194
225
|
end
|
195
|
-
|
196
|
-
return buffer
|
197
226
|
end
|
198
227
|
|
199
228
|
##
|
@@ -205,12 +234,14 @@ class String
|
|
205
234
|
# @see #map
|
206
235
|
#
|
207
236
|
|
208
|
-
|
209
|
-
|
210
|
-
self
|
237
|
+
if not self.__hash_utils_instance_respond_to? :map!
|
238
|
+
def map!(&block)
|
239
|
+
self.length.times do |i|
|
240
|
+
self[i] = block.call(self[i])
|
241
|
+
end
|
242
|
+
|
243
|
+
return self
|
211
244
|
end
|
212
|
-
|
213
|
-
self
|
214
245
|
end
|
215
246
|
|
216
247
|
##
|
@@ -227,12 +258,14 @@ class String
|
|
227
258
|
# @since 0.8.0
|
228
259
|
#
|
229
260
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
261
|
+
if not self.__hash_utils_instance_respond_to? :gsub_f
|
262
|
+
def gsub_f(from, to = nil, &block)
|
263
|
+
__prepare_gsub_f(from, to, block) do |callback|
|
264
|
+
if to.nil?
|
265
|
+
self.gsub(from, &callback)
|
266
|
+
else
|
267
|
+
self.gsub(from, to)
|
268
|
+
end
|
236
269
|
end
|
237
270
|
end
|
238
271
|
end
|
@@ -249,16 +282,18 @@ class String
|
|
249
282
|
# @since 0.8.0
|
250
283
|
#
|
251
284
|
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
285
|
+
if not self.__hash_utils_instance_respond_to? :gsub_f!
|
286
|
+
def gsub_f!(from, to = nil, &block)
|
287
|
+
__prepare_gsub_f(from, to, block) do |callback|
|
288
|
+
if to.nil?
|
289
|
+
self.gsub!(from, &callback)
|
290
|
+
else
|
291
|
+
self.gsub!(from, to)
|
292
|
+
end
|
258
293
|
end
|
294
|
+
|
295
|
+
return self
|
259
296
|
end
|
260
|
-
|
261
|
-
self
|
262
297
|
end
|
263
298
|
|
264
299
|
##
|
@@ -271,8 +306,10 @@ class String
|
|
271
306
|
# @since 0.11.0
|
272
307
|
#
|
273
308
|
|
274
|
-
|
275
|
-
|
309
|
+
if not self.__hash_utils_instance_respond_to? :first
|
310
|
+
def first
|
311
|
+
self.chr
|
312
|
+
end
|
276
313
|
end
|
277
314
|
|
278
315
|
##
|
@@ -282,8 +319,10 @@ class String
|
|
282
319
|
# @since 0.15.0
|
283
320
|
#
|
284
321
|
|
285
|
-
|
286
|
-
|
322
|
+
if not self.__hash_utils_instance_respond_to? :second
|
323
|
+
def second
|
324
|
+
self[1].chr
|
325
|
+
end
|
287
326
|
end
|
288
327
|
|
289
328
|
##
|
@@ -293,8 +332,10 @@ class String
|
|
293
332
|
# @since 0.15.0
|
294
333
|
#
|
295
334
|
|
296
|
-
|
297
|
-
|
335
|
+
if not self.__hash_utils_instance_respond_to? :third
|
336
|
+
def third
|
337
|
+
self[2].chr
|
338
|
+
end
|
298
339
|
end
|
299
340
|
|
300
341
|
##
|
@@ -304,8 +345,10 @@ class String
|
|
304
345
|
# @since 0.15.0
|
305
346
|
#
|
306
347
|
|
307
|
-
|
308
|
-
|
348
|
+
if not self.__hash_utils_instance_respond_to? :fourth
|
349
|
+
def fourth
|
350
|
+
self[3].chr
|
351
|
+
end
|
309
352
|
end
|
310
353
|
|
311
354
|
##
|
@@ -315,8 +358,10 @@ class String
|
|
315
358
|
# @since 0.15.0
|
316
359
|
#
|
317
360
|
|
318
|
-
|
319
|
-
|
361
|
+
if not self.__hash_utils_instance_respond_to? :fifth
|
362
|
+
def fifth
|
363
|
+
self[4].chr
|
364
|
+
end
|
320
365
|
end
|
321
366
|
|
322
367
|
##
|
@@ -326,8 +371,10 @@ class String
|
|
326
371
|
# @since 0.15.0
|
327
372
|
#
|
328
373
|
|
329
|
-
|
330
|
-
|
374
|
+
if not self.__hash_utils_instance_respond_to? :sixth
|
375
|
+
def sixth
|
376
|
+
self[5].chr
|
377
|
+
end
|
331
378
|
end
|
332
379
|
|
333
380
|
##
|
@@ -337,8 +384,10 @@ class String
|
|
337
384
|
# @since 0.15.0
|
338
385
|
#
|
339
386
|
|
340
|
-
|
341
|
-
|
387
|
+
if not self.__hash_utils_instance_respond_to? :seventh
|
388
|
+
def seventh
|
389
|
+
self[6].chr
|
390
|
+
end
|
342
391
|
end
|
343
392
|
|
344
393
|
##
|
@@ -348,8 +397,10 @@ class String
|
|
348
397
|
# @since 0.15.0
|
349
398
|
#
|
350
399
|
|
351
|
-
|
352
|
-
|
400
|
+
if not self.__hash_utils_instance_respond_to? :eighth
|
401
|
+
def eighth
|
402
|
+
self[7].chr
|
403
|
+
end
|
353
404
|
end
|
354
405
|
|
355
406
|
##
|
@@ -362,8 +413,10 @@ class String
|
|
362
413
|
# @since 0.11.0
|
363
414
|
#
|
364
415
|
|
365
|
-
|
366
|
-
|
416
|
+
if not self.__hash_utils_instance_respond_to? :last
|
417
|
+
def last
|
418
|
+
self[-1].chr
|
419
|
+
end
|
367
420
|
end
|
368
421
|
|
369
422
|
##
|
@@ -380,15 +433,17 @@ class String
|
|
380
433
|
# @since 0.12.0
|
381
434
|
#
|
382
435
|
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
436
|
+
if not self.__hash_utils_instance_respond_to? :first_lines
|
437
|
+
def first_lines(count = 1)
|
438
|
+
result = [ ]
|
439
|
+
self.each_line do |line|
|
440
|
+
count -= 1
|
441
|
+
result << line
|
442
|
+
break if count == 0
|
443
|
+
end
|
444
|
+
|
445
|
+
return result
|
389
446
|
end
|
390
|
-
|
391
|
-
return result
|
392
447
|
end
|
393
448
|
|
394
449
|
##
|
@@ -403,9 +458,11 @@ class String
|
|
403
458
|
# @return [String] line with +\n+
|
404
459
|
# @since 0.12.0
|
405
460
|
#
|
406
|
-
|
407
|
-
|
408
|
-
|
461
|
+
|
462
|
+
if not self.__hash_utils_instance_respond_to? :first_line
|
463
|
+
def first_line
|
464
|
+
self.first_lines.first
|
465
|
+
end
|
409
466
|
end
|
410
467
|
|
411
468
|
##
|
@@ -422,24 +479,26 @@ class String
|
|
422
479
|
# @since 0.12.0
|
423
480
|
#
|
424
481
|
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
482
|
+
if not self.__hash_utils_instance_respond_to? :last_lines
|
483
|
+
def last_lines(count = 1)
|
484
|
+
buffer = ""
|
485
|
+
result = [ ]
|
486
|
+
(self.length - 1).downto(0) do |i|
|
487
|
+
chr = self[i]
|
488
|
+
if chr.ord == 10
|
489
|
+
count -= 1
|
490
|
+
result << buffer.reverse!
|
491
|
+
buffer = ""
|
492
|
+
break if count == 0
|
493
|
+
end
|
494
|
+
buffer << chr.chr
|
495
|
+
end
|
496
|
+
|
497
|
+
if count != 0
|
432
498
|
result << buffer.reverse!
|
433
|
-
buffer = ""
|
434
|
-
break if count == 0
|
435
499
|
end
|
436
|
-
|
437
|
-
end
|
438
|
-
|
439
|
-
if count != 0
|
440
|
-
result << buffer.reverse!
|
500
|
+
return result.reverse!
|
441
501
|
end
|
442
|
-
return result.reverse!
|
443
502
|
end
|
444
503
|
|
445
504
|
##
|
@@ -454,9 +513,11 @@ class String
|
|
454
513
|
# @return [String] line
|
455
514
|
# @since 0.12.0
|
456
515
|
#
|
457
|
-
|
458
|
-
|
459
|
-
|
516
|
+
|
517
|
+
if not self.__hash_utils_instance_respond_to? :last_line
|
518
|
+
def last_line
|
519
|
+
self.last_lines.last
|
520
|
+
end
|
460
521
|
end
|
461
522
|
|
462
523
|
##
|
@@ -477,22 +538,28 @@ class String
|
|
477
538
|
# @since 0.12.0
|
478
539
|
#
|
479
540
|
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
541
|
+
if not self.__hash_utils_instance_respond_to? :shift_lines
|
542
|
+
def shift_lines(count = 1)
|
543
|
+
lines = self.first_lines(count)
|
544
|
+
length = lines.reduce(0) { |sum, i| sum + i.length }
|
545
|
+
self.replace(self[length..-1])
|
546
|
+
return lines
|
547
|
+
end
|
485
548
|
end
|
486
549
|
|
487
550
|
##
|
488
551
|
# Removes first line out from the string and returns it.
|
552
|
+
#
|
489
553
|
# @return [String] removed line
|
554
|
+
# @since 0.12.0
|
490
555
|
#
|
491
556
|
|
492
|
-
|
493
|
-
|
557
|
+
if not self.__hash_utils_instance_respond_to? :shift_line
|
558
|
+
def shift_line
|
559
|
+
self.shift_lines.first
|
560
|
+
end
|
494
561
|
end
|
495
|
-
|
562
|
+
|
496
563
|
##
|
497
564
|
# Puts lines to begin of string.
|
498
565
|
#
|
@@ -501,11 +568,15 @@ class String
|
|
501
568
|
# @since 0.12.0
|
502
569
|
#
|
503
570
|
|
504
|
-
|
505
|
-
|
571
|
+
if not self.__hash_utils_instance_respond_to? :unshift_lines
|
572
|
+
def unshift_lines(*lines)
|
573
|
+
self.unshift(lines.join("\n") << "\n")
|
574
|
+
end
|
506
575
|
end
|
507
576
|
|
508
|
-
|
577
|
+
if not self.__hash_utils_instance_respond_to? :unshift_line
|
578
|
+
alias :unshift_line :unshift_lines
|
579
|
+
end
|
509
580
|
|
510
581
|
##
|
511
582
|
# Removes lines out of end of the string.
|
@@ -517,11 +588,13 @@ class String
|
|
517
588
|
# @since 0.12.0
|
518
589
|
#
|
519
590
|
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
591
|
+
if not self.__hash_utils_instance_respond_to? :pop_lines
|
592
|
+
def pop_lines(count = 1)
|
593
|
+
lines = self.last_lines(count)
|
594
|
+
length = lines.inject(0) { |sum, i| sum + i.length }
|
595
|
+
self.replace(self[0..(length - 1)])
|
596
|
+
return lines
|
597
|
+
end
|
525
598
|
end
|
526
599
|
|
527
600
|
##
|
@@ -529,8 +602,10 @@ class String
|
|
529
602
|
# @return [String] removed line
|
530
603
|
#
|
531
604
|
|
532
|
-
|
533
|
-
|
605
|
+
if not self.__hash_utils_instance_respond_to? :pop_line
|
606
|
+
def pop_line
|
607
|
+
self.pop_lines.first
|
608
|
+
end
|
534
609
|
end
|
535
610
|
|
536
611
|
##
|
@@ -540,13 +615,20 @@ class String
|
|
540
615
|
# @return [String] itself
|
541
616
|
# @since 0.12.0
|
542
617
|
#
|
543
|
-
|
544
|
-
|
545
|
-
|
618
|
+
|
619
|
+
if not self.__hash_utils_instance_respond_to? :push_lines
|
620
|
+
def push_lines(*lines)
|
621
|
+
self.push("\n" << lines.join("\n"))
|
622
|
+
end
|
623
|
+
end
|
624
|
+
|
625
|
+
if not self.__hash_utils_instance_respond_to? :push_line
|
626
|
+
alias :push_line :push_lines
|
546
627
|
end
|
547
628
|
|
548
|
-
|
549
|
-
|
629
|
+
if not self.__hash_utils_instance_respond_to? :push
|
630
|
+
alias :push :<<
|
631
|
+
end
|
550
632
|
|
551
633
|
##
|
552
634
|
# Removes appropriate number of characters from end of string.
|
@@ -556,12 +638,14 @@ class String
|
|
556
638
|
# @since 0.12.0
|
557
639
|
#
|
558
640
|
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
641
|
+
if not self.__hash_utils_instance_respond_to? :pop
|
642
|
+
def pop(count = 1)
|
643
|
+
res = self[(self.length - count)..-1]
|
644
|
+
self.replace(self[0..-(count + 1)])
|
645
|
+
return res
|
646
|
+
end
|
563
647
|
end
|
564
|
-
|
648
|
+
|
565
649
|
##
|
566
650
|
# Removes appropriate number of characters from begin of string.
|
567
651
|
#
|
@@ -570,10 +654,12 @@ class String
|
|
570
654
|
# @since 0.12.0
|
571
655
|
#
|
572
656
|
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
657
|
+
if not self.__hash_utils_instance_respond_to? :shift
|
658
|
+
def shift(count = 1)
|
659
|
+
res = self[0...count]
|
660
|
+
self.replace(self[count..-1])
|
661
|
+
return res
|
662
|
+
end
|
577
663
|
end
|
578
664
|
|
579
665
|
if Ruby::Version < [1, 9, 3]
|
@@ -588,16 +674,24 @@ class String
|
|
588
674
|
# @since 0.12.0
|
589
675
|
#
|
590
676
|
|
591
|
-
|
592
|
-
|
677
|
+
if not self.__hash_utils_instance_respond_to? :unshift
|
678
|
+
def unshift(string)
|
679
|
+
self.replace(string + self)
|
680
|
+
end
|
593
681
|
end
|
594
682
|
|
595
|
-
|
683
|
+
if not self.__hash_utils_instance_respond_to? :prepend
|
684
|
+
alias :prepend :unshift
|
685
|
+
end
|
596
686
|
else
|
597
|
-
|
687
|
+
if not self.__hash_utils_instance_respond_to? :unshift
|
688
|
+
alias :unshift :prepend
|
689
|
+
end
|
598
690
|
end
|
599
691
|
|
600
|
-
|
692
|
+
if not self.__hash_utils_instance_respond_to? :append
|
693
|
+
alias :append :<<
|
694
|
+
end
|
601
695
|
|
602
696
|
##
|
603
697
|
# Converts first character of the string to uppercase.
|
@@ -607,8 +701,10 @@ class String
|
|
607
701
|
# @since 0.15.0
|
608
702
|
#
|
609
703
|
|
610
|
-
|
611
|
-
|
704
|
+
if not self.__hash_utils_instance_respond_to? :ucfirst
|
705
|
+
def ucfirst
|
706
|
+
self.dup.ucfirst!
|
707
|
+
end
|
612
708
|
end
|
613
709
|
|
614
710
|
##
|
@@ -619,9 +715,11 @@ class String
|
|
619
715
|
# @since 0.15.0
|
620
716
|
#
|
621
717
|
|
622
|
-
|
623
|
-
|
624
|
-
|
718
|
+
if not self.__hash_utils_instance_respond_to? :ucfirst!
|
719
|
+
def ucfirst!
|
720
|
+
self[0] = self.first.upcase
|
721
|
+
return self
|
722
|
+
end
|
625
723
|
end
|
626
724
|
|
627
725
|
##
|
@@ -632,8 +730,10 @@ class String
|
|
632
730
|
# @since 0.15.0
|
633
731
|
#
|
634
732
|
|
635
|
-
|
636
|
-
|
733
|
+
if not self.__hash_utils_instance_respond_to? :lcfirst
|
734
|
+
def lcfirst
|
735
|
+
self.dup.lcfirst!
|
736
|
+
end
|
637
737
|
end
|
638
738
|
|
639
739
|
##
|
@@ -644,9 +744,11 @@ class String
|
|
644
744
|
# @since 0.15.0
|
645
745
|
#
|
646
746
|
|
647
|
-
|
648
|
-
|
649
|
-
|
747
|
+
if not self.__hash_utils_instance_respond_to? :lcfirst!
|
748
|
+
def lcfirst!
|
749
|
+
self[0] = self.first.downcase
|
750
|
+
return self
|
751
|
+
end
|
650
752
|
end
|
651
753
|
|
652
754
|
##
|
@@ -656,8 +758,10 @@ class String
|
|
656
758
|
# @since 0.17.0
|
657
759
|
#
|
658
760
|
|
659
|
-
|
660
|
-
|
761
|
+
if not self.__hash_utils_instance_respond_to? :string?
|
762
|
+
def string?
|
763
|
+
true
|
764
|
+
end
|
661
765
|
end
|
662
766
|
|
663
767
|
##
|
@@ -668,14 +772,18 @@ class String
|
|
668
772
|
# @since 0.18.0
|
669
773
|
#
|
670
774
|
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
775
|
+
if not self.__hash_utils_instance_respond_to? :swap_with
|
776
|
+
def swap_with(from)
|
777
|
+
intermediate = self.dup
|
778
|
+
self.replace(from)
|
779
|
+
from.replace(intermediate)
|
780
|
+
return self
|
781
|
+
end
|
676
782
|
end
|
677
783
|
|
678
|
-
|
784
|
+
if not self.__hash_utils_instance_respond_to? :swap_with!
|
785
|
+
alias :swap_with! :swap_with
|
786
|
+
end
|
679
787
|
|
680
788
|
##
|
681
789
|
# Cuts string in place. Sets the content of #[] on place of
|
@@ -686,8 +794,10 @@ class String
|
|
686
794
|
# @since 0.18.0
|
687
795
|
#
|
688
796
|
|
689
|
-
|
690
|
-
|
797
|
+
if not self.__hash_utils_instance_respond_to? :cut!
|
798
|
+
def cut!(range)
|
799
|
+
self.replace(self[range])
|
800
|
+
end
|
691
801
|
end
|
692
802
|
|
693
803
|
##
|
@@ -702,8 +812,10 @@ class String
|
|
702
812
|
# @since 0.18.1
|
703
813
|
#
|
704
814
|
|
705
|
-
|
706
|
-
|
815
|
+
if not self.__hash_utils_instance_respond_to? :interlace
|
816
|
+
def interlace(string)
|
817
|
+
self.gsub(self.class::INTERLACING, '\1' << string << '\2' << string)
|
818
|
+
end
|
707
819
|
end
|
708
820
|
|
709
821
|
##
|
@@ -715,9 +827,11 @@ class String
|
|
715
827
|
# @since 0.18.1
|
716
828
|
#
|
717
829
|
|
718
|
-
|
719
|
-
|
720
|
-
|
830
|
+
if not self.__hash_utils_instance_respond_to? :interlace!
|
831
|
+
def interlace!(string)
|
832
|
+
self.gsub!(self.class::INTERLACING, '\1' << string << '\2' << string)
|
833
|
+
return self
|
834
|
+
end
|
721
835
|
end
|
722
836
|
|
723
837
|
##
|
@@ -731,8 +845,10 @@ class String
|
|
731
845
|
# @since 0.19.0
|
732
846
|
#
|
733
847
|
|
734
|
-
|
735
|
-
|
848
|
+
if not self.__hash_utils_instance_respond_to? :to_boolean
|
849
|
+
def to_boolean(t = "true")
|
850
|
+
self == t
|
851
|
+
end
|
736
852
|
end
|
737
853
|
|
738
854
|
|
@@ -747,7 +863,7 @@ class String
|
|
747
863
|
keys = defs.keys
|
748
864
|
keys.map! { |i| i.to_s }
|
749
865
|
|
750
|
-
matcher = Regexp::new("("
|
866
|
+
matcher = Regexp::new("(" + keys.join("|") + ")")
|
751
867
|
return [defs, matcher]
|
752
868
|
end
|
753
869
|
|