lite-ruby 1.0.14 → 1.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/Gemfile.lock +1 -1
- data/lib/lite/ruby/array.rb +221 -219
- data/lib/lite/ruby/boolean.rb +18 -16
- data/lib/lite/ruby/date.rb +18 -16
- data/lib/lite/ruby/enumerable.rb +169 -167
- data/lib/lite/ruby/hash.rb +309 -307
- data/lib/lite/ruby/helpers/date_helper.rb +97 -95
- data/lib/lite/ruby/helpers/time_helper.rb +79 -77
- data/lib/lite/ruby/integer.rb +57 -55
- data/lib/lite/ruby/kernel.rb +21 -19
- data/lib/lite/ruby/numeric.rb +189 -187
- data/lib/lite/ruby/object.rb +138 -136
- data/lib/lite/ruby/open_struct.rb +30 -28
- data/lib/lite/ruby/range.rb +23 -21
- data/lib/lite/ruby/string.rb +495 -493
- data/lib/lite/ruby/struct.rb +15 -13
- data/lib/lite/ruby/time.rb +27 -25
- data/lib/lite/ruby/version.rb +1 -1
- metadata +1 -1
data/lib/lite/ruby/string.rb
CHANGED
@@ -1,632 +1,634 @@
|
|
1
1
|
# frozen_string_literal: false
|
2
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
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('string')
|
4
|
+
class String
|
5
|
+
|
6
|
+
TRANSLITERATIONS ||= {
|
7
|
+
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'Ae',
|
8
|
+
'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I',
|
9
|
+
'Î' => 'I', 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O',
|
10
|
+
'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U',
|
11
|
+
'Ý' => 'Y', 'Þ' => 'Th', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a',
|
12
|
+
'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e',
|
13
|
+
'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n',
|
14
|
+
'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u',
|
15
|
+
'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y', 'Ā' => 'A',
|
16
|
+
'ā' => 'a', 'Ă' => 'A', 'ă' => 'a', 'Ą' => 'A', 'ą' => 'a', 'Ć' => 'C', 'ć' => 'c',
|
17
|
+
'Ĉ' => 'C', 'ĉ' => 'c', 'Ċ' => 'C', 'ċ' => 'c', 'Č' => 'C', 'č' => 'c', 'Ď' => 'D',
|
18
|
+
'ď' => 'd', 'Đ' => 'D', 'đ' => 'd', 'Ē' => 'E', 'ē' => 'e', 'Ĕ' => 'E', 'ĕ' => 'e',
|
19
|
+
'Ė' => 'E', 'ė' => 'e', 'Ę' => 'E', 'ę' => 'e', 'Ě' => 'E', 'ě' => 'e', 'Ĝ' => 'G',
|
20
|
+
'ĝ' => 'g', 'Ğ' => 'G', 'ğ' => 'g', 'Ġ' => 'G', 'ġ' => 'g', 'Ģ' => 'G', 'ģ' => 'g',
|
21
|
+
'Ĥ' => 'H', 'ĥ' => 'h', 'Ħ' => 'H', 'ħ' => 'h', 'Ĩ' => 'I', 'ĩ' => 'i', 'Ī' => 'I',
|
22
|
+
'ī' => 'i', 'Ĭ' => 'I', 'ĭ' => 'i', 'Į' => 'I', 'į' => 'i', 'İ' => 'I', 'ı' => 'i',
|
23
|
+
'IJ' => 'Ij', 'ij' => 'ij', 'Ĵ' => 'J', 'ĵ' => 'j', 'Ķ' => 'K', 'ķ' => 'k', 'ĸ' => 'k',
|
24
|
+
'Ĺ' => 'L', 'ĺ' => 'l', 'Ļ' => 'L', 'ļ' => 'l', 'Ľ' => 'L', 'ľ' => 'l', 'Ŀ' => 'L',
|
25
|
+
'ŀ' => 'l', 'Ł' => 'L', 'ł' => 'l', 'Ń' => 'N', 'ń' => 'n', 'Ņ' => 'N', 'ņ' => 'n',
|
26
|
+
'Ň' => 'N', 'ň' => 'n', 'ʼn' => 'n', 'Ŋ' => 'Ng', 'ŋ' => 'ng', 'Ō' => 'O', 'ō' => 'o',
|
27
|
+
'Ŏ' => 'O', 'ŏ' => 'o', 'Ő' => 'O', 'ő' => 'o', 'Œ' => 'OE', 'œ' => 'oe', 'Ŕ' => 'R',
|
28
|
+
'ŕ' => 'r', 'Ŗ' => 'R', 'ŗ' => 'r', 'Ř' => 'R', 'ř' => 'r', 'Ś' => 'S', 'ś' => 's',
|
29
|
+
'Ŝ' => 'S', 'ŝ' => 's', 'Ş' => 'S', 'ş' => 's', 'Š' => 'S', 'š' => 's', 'Ţ' => 'T',
|
30
|
+
'ţ' => 't', 'Ť' => 'T', 'ť' => 't', 'Ŧ' => 'T', 'ŧ' => 't', 'Ũ' => 'U', 'ũ' => 'u',
|
31
|
+
'Ū' => 'U', 'ū' => 'u', 'Ŭ' => 'U', 'ŭ' => 'u', 'Ů' => 'U', 'ů' => 'u', 'Ű' => 'U',
|
32
|
+
'ű' => 'u', 'Ų' => 'U', 'ų' => 'u', 'Ŵ' => 'W', 'ŵ' => 'w', 'Ŷ' => 'Y', 'ŷ' => 'y',
|
33
|
+
'Ÿ' => 'Y', 'Ź' => 'Z', 'ź' => 'z', 'Ż' => 'Z', 'ż' => 'z', 'Ž' => 'Z', 'ž' => 'z'
|
34
|
+
}.freeze
|
35
|
+
|
36
|
+
def acronym
|
37
|
+
dup.acronym!
|
38
|
+
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
def acronym!
|
41
|
+
gsub!(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./, '\\2') || self
|
42
|
+
end
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
def any?(*keys)
|
45
|
+
keys.any? { |key| include?(key) }
|
46
|
+
end
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
48
|
+
def ascii_only(alt = '')
|
49
|
+
dup.ascii_only!(alt)
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
52
|
+
def ascii_only!(alt = '')
|
53
|
+
encode_only!('ASCII', alt)
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
56
|
+
def at(position)
|
57
|
+
self[position]
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
60
|
+
def camelize(first_letter = :upper)
|
61
|
+
case first_letter
|
62
|
+
when :upper, true then modulize.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase }
|
63
|
+
when :lower, false then modulize.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase }
|
64
|
+
end || modulize
|
65
|
+
end
|
65
66
|
|
66
|
-
|
67
|
+
alias camelcase camelize
|
67
68
|
|
68
|
-
|
69
|
-
|
70
|
-
|
69
|
+
def camelize!(first_letter = :upper)
|
70
|
+
replace(camelize(first_letter))
|
71
|
+
end
|
71
72
|
|
72
|
-
|
73
|
+
alias camelcase! camelize!
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
75
|
+
def capitalized?
|
76
|
+
capitalize == self
|
77
|
+
end
|
77
78
|
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
def classify
|
80
|
+
dup.classify!
|
81
|
+
end
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
def classify!
|
84
|
+
sub!(/.*\./, '')
|
85
|
+
camelize!
|
86
|
+
end
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
88
|
+
def constantize
|
89
|
+
Object.const_get(camelize)
|
90
|
+
end
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
|
92
|
+
def dasherize
|
93
|
+
underscore.tr('_', '-')
|
94
|
+
end
|
94
95
|
|
95
|
-
|
96
|
-
|
97
|
-
|
96
|
+
def dasherize!
|
97
|
+
replace(dasherize)
|
98
|
+
end
|
98
99
|
|
99
|
-
|
100
|
-
|
101
|
-
|
100
|
+
def deconstantize
|
101
|
+
[0, rindex('::') || 0]
|
102
|
+
end
|
102
103
|
|
103
|
-
|
104
|
-
|
105
|
-
|
104
|
+
def deconstantize!
|
105
|
+
replace(deconstantize)
|
106
|
+
end
|
106
107
|
|
107
|
-
|
108
|
-
|
109
|
-
|
108
|
+
def dedupe(pattern)
|
109
|
+
dup.dedupe!(pattern)
|
110
|
+
end
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
112
|
+
def dedupe!(pattern)
|
113
|
+
pattern.each_char { |char| gsub!(/#{Regexp.escape(char)}{2,}/, char) }
|
114
|
+
self
|
115
|
+
end
|
115
116
|
|
116
|
-
|
117
|
-
|
118
|
-
|
117
|
+
def demodulize
|
118
|
+
gsub(/^.*::/, '')
|
119
|
+
end
|
119
120
|
|
120
|
-
|
121
|
-
|
122
|
-
|
121
|
+
def demodulize!
|
122
|
+
replace(demodulize)
|
123
|
+
end
|
123
124
|
|
124
|
-
|
125
|
-
|
125
|
+
def domain
|
126
|
+
return self unless self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)}
|
126
127
|
|
127
|
-
|
128
|
-
|
128
|
+
Regexp.last_match(1)
|
129
|
+
end
|
129
130
|
|
130
|
-
|
131
|
-
|
132
|
-
|
131
|
+
def downcase?
|
132
|
+
downcase == self
|
133
|
+
end
|
133
134
|
|
134
|
-
|
135
|
-
|
136
|
-
|
135
|
+
def each_word(&block)
|
136
|
+
words.each(&block)
|
137
|
+
end
|
137
138
|
|
138
|
-
|
139
|
-
|
140
|
-
|
139
|
+
def encode_only(encoding, alt = '')
|
140
|
+
dup.encode_only!(encoding, alt)
|
141
|
+
end
|
141
142
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
143
|
+
def encode_only!(encoding, alt = '')
|
144
|
+
encoding_options = {
|
145
|
+
invalid: :replace,
|
146
|
+
undef: :replace,
|
147
|
+
replace: alt,
|
148
|
+
UNIVERSAL_NEWLINE_DECORATOR: true
|
149
|
+
}
|
149
150
|
|
150
|
-
|
151
|
-
|
151
|
+
encode!(Encoding.find(encoding), encoding_options)
|
152
|
+
end
|
152
153
|
|
153
|
-
|
154
|
-
|
154
|
+
def ellipsize(ellipsize_at, options = {})
|
155
|
+
return self if length <= ellipsize_at
|
155
156
|
|
156
|
-
|
157
|
-
|
157
|
+
separator = options[:separator] || '...'
|
158
|
+
offset = options[:offset] || 4
|
158
159
|
|
159
|
-
|
160
|
-
|
160
|
+
"#{self[0, offset]}#{separator}#{self[-offset, offset]}"
|
161
|
+
end
|
161
162
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
163
|
+
def first(limit = 1)
|
164
|
+
if limit.zero?
|
165
|
+
''
|
166
|
+
elsif limit >= length
|
167
|
+
self
|
168
|
+
else
|
169
|
+
to(limit - 1)
|
170
|
+
end
|
169
171
|
end
|
170
|
-
end
|
171
172
|
|
172
|
-
|
173
|
-
|
174
|
-
|
173
|
+
def format(*args)
|
174
|
+
super(self, *args.flatten)
|
175
|
+
end
|
175
176
|
|
176
|
-
|
177
|
-
|
178
|
-
|
177
|
+
def from(position)
|
178
|
+
self[position..-1]
|
179
|
+
end
|
179
180
|
|
180
|
-
|
181
|
-
|
182
|
-
|
181
|
+
def headerize
|
182
|
+
squish.each_word(&:capitalize!).join(' ')
|
183
|
+
end
|
183
184
|
|
184
|
-
|
185
|
-
|
186
|
-
|
185
|
+
def headerize!
|
186
|
+
replace(headerize)
|
187
|
+
end
|
187
188
|
|
188
|
-
|
189
|
-
|
190
|
-
|
189
|
+
def humanize(options = {})
|
190
|
+
dup.humanize!(options)
|
191
|
+
end
|
191
192
|
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
193
|
+
def humanize!(capitalize: true)
|
194
|
+
underscore!
|
195
|
+
gsub!(/_id\z/, '')
|
196
|
+
tr!('_', ' ')
|
197
|
+
squish!
|
198
|
+
gsub!(/([a-z\d]*)/i, &:downcase)
|
199
|
+
gsub!(/\A\w/) { |str| capitalize ? str.upcase : str } || self
|
200
|
+
end
|
200
201
|
|
201
|
-
|
202
|
-
|
203
|
-
|
202
|
+
def indent(amount, seperator = ' ')
|
203
|
+
dup.indent!(amount, seperator)
|
204
|
+
end
|
204
205
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
206
|
+
def indent!(amount, seperator = ' ')
|
207
|
+
if amount >= 0
|
208
|
+
gsub!(/^/, seperator * amount)
|
209
|
+
else
|
210
|
+
gsub!(/^#{Regexp.escape(seperator)}{0,#{-amount}}/, '')
|
211
|
+
end
|
210
212
|
end
|
211
|
-
end
|
212
213
|
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
214
|
+
def index_all(pattern)
|
215
|
+
pattern = pattern.to_s if pattern.is_a?(Numeric)
|
216
|
+
arr_indexes = []
|
217
|
+
srch_index = rindex(pattern)
|
217
218
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
219
|
+
while srch_index
|
220
|
+
temp_string = self[0..(srch_index - 1)]
|
221
|
+
arr_indexes << srch_index
|
222
|
+
srch_index = srch_index.zero? ? nil : temp_string.rindex(pattern)
|
223
|
+
end
|
224
|
+
|
225
|
+
arr_indexes.reverse
|
222
226
|
end
|
223
227
|
|
224
|
-
|
225
|
-
|
228
|
+
def labelize(options = {})
|
229
|
+
dup.labelize!(options)
|
230
|
+
end
|
226
231
|
|
227
|
-
|
228
|
-
dup.labelize!(options)
|
229
|
-
end
|
232
|
+
alias labelcase labelize
|
230
233
|
|
231
|
-
|
234
|
+
def labelize!(capitalize: true)
|
235
|
+
underscore!
|
236
|
+
tr!('_', ' ')
|
237
|
+
squish!
|
238
|
+
gsub!(/([a-z\d]*)/i, &:downcase)
|
239
|
+
gsub!(/\A\w/) { |str| capitalize ? str.upcase : str }
|
240
|
+
gsub!(/ id\z/, ' ID') || self
|
241
|
+
end
|
232
242
|
|
233
|
-
|
234
|
-
underscore!
|
235
|
-
tr!('_', ' ')
|
236
|
-
squish!
|
237
|
-
gsub!(/([a-z\d]*)/i, &:downcase)
|
238
|
-
gsub!(/\A\w/) { |str| capitalize ? str.upcase : str }
|
239
|
-
gsub!(/ id\z/, ' ID') || self
|
240
|
-
end
|
243
|
+
alias labelcase! labelize!
|
241
244
|
|
242
|
-
|
245
|
+
def last(limit = 1)
|
246
|
+
if limit.zero?
|
247
|
+
''
|
248
|
+
elsif limit >= length
|
249
|
+
self
|
250
|
+
else
|
251
|
+
from(-limit)
|
252
|
+
end
|
253
|
+
end
|
243
254
|
|
244
|
-
|
245
|
-
|
246
|
-
''
|
247
|
-
elsif limit >= length
|
248
|
-
self
|
249
|
-
else
|
250
|
-
from(-limit)
|
255
|
+
def lchomp(match)
|
256
|
+
dup.lchomp!(match)
|
251
257
|
end
|
252
|
-
end
|
253
258
|
|
254
|
-
|
255
|
-
|
256
|
-
end
|
259
|
+
def lchomp!(match)
|
260
|
+
return self unless index(match)
|
257
261
|
|
258
|
-
|
259
|
-
|
262
|
+
self[0...match.size] = ''
|
263
|
+
self
|
264
|
+
end
|
260
265
|
|
261
|
-
|
262
|
-
|
263
|
-
|
266
|
+
def methodize
|
267
|
+
dup.methodize!
|
268
|
+
end
|
264
269
|
|
265
|
-
|
266
|
-
|
267
|
-
|
270
|
+
def methodize!
|
271
|
+
gsub!(/([A-Z]+)([A-Z])/, '\1_\2')
|
272
|
+
gsub!(/([a-z])([A-Z])/, '\1_\2')
|
273
|
+
gsub!('/', '__')
|
274
|
+
gsub!('::', '__')
|
275
|
+
downcase! || self
|
276
|
+
end
|
268
277
|
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
gsub!('/', '__')
|
273
|
-
gsub!('::', '__')
|
274
|
-
downcase! || self
|
275
|
-
end
|
278
|
+
def modulize
|
279
|
+
dup.modulize!
|
280
|
+
end
|
276
281
|
|
277
|
-
|
278
|
-
|
279
|
-
|
282
|
+
def modulize!
|
283
|
+
gsub!(/__(.?)/) { "::#{$1.upcase}" }
|
284
|
+
gsub!(%r{/(.?)}) { "::#{$1.upcase}" }
|
285
|
+
gsub!(/(?:_+|-+)([a-z])/) { $1.upcase }
|
286
|
+
gsub!(/(\A|\s)([a-z])/) { $1 + $2.upcase } || self
|
287
|
+
end
|
280
288
|
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
gsub!(/(?:_+|-+)([a-z])/) { $1.upcase }
|
285
|
-
gsub!(/(\A|\s)([a-z])/) { $1 + $2.upcase } || self
|
286
|
-
end
|
289
|
+
def mixedcase?
|
290
|
+
!upcase? && !downcase?
|
291
|
+
end
|
287
292
|
|
288
|
-
|
289
|
-
|
290
|
-
|
293
|
+
def ordinal
|
294
|
+
to_i.ordinal
|
295
|
+
end
|
291
296
|
|
292
|
-
|
293
|
-
|
294
|
-
|
297
|
+
def ordinalize
|
298
|
+
to_i.ordinalize
|
299
|
+
end
|
295
300
|
|
296
|
-
|
297
|
-
|
298
|
-
|
301
|
+
def parameterize(separator: '-')
|
302
|
+
dup.parameterize!(separator: separator)
|
303
|
+
end
|
299
304
|
|
300
|
-
|
301
|
-
|
302
|
-
|
305
|
+
def parameterize!(separator: '-')
|
306
|
+
underscore!
|
307
|
+
gsub!(/\s+/, separator)
|
308
|
+
downcase! || self
|
309
|
+
end
|
303
310
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
downcase! || self
|
308
|
-
end
|
311
|
+
def pathize
|
312
|
+
dup.pathize!
|
313
|
+
end
|
309
314
|
|
310
|
-
|
311
|
-
|
312
|
-
|
315
|
+
def pathize!
|
316
|
+
gsub!(/([A-Z]+)([A-Z])/, '\1_\2')
|
317
|
+
gsub!(/([a-z])([A-Z])/, '\1_\2')
|
318
|
+
gsub!('__', '/')
|
319
|
+
gsub!('::', '/')
|
320
|
+
gsub!(/\s+/, '')
|
321
|
+
gsub!(/[?%*:|"<>.]+/, '')
|
322
|
+
downcase! || self
|
323
|
+
end
|
313
324
|
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
gsub!('__', '/')
|
318
|
-
gsub!('::', '/')
|
319
|
-
gsub!(/\s+/, '')
|
320
|
-
gsub!(/[?%*:|"<>.]+/, '')
|
321
|
-
downcase! || self
|
322
|
-
end
|
325
|
+
def pollute(delimiter = '^--^--^')
|
326
|
+
chars.map { |chr| "#{chr}#{delimiter}" }.join
|
327
|
+
end
|
323
328
|
|
324
|
-
|
325
|
-
|
326
|
-
|
329
|
+
def pollute!(delimiter = '^--^--^')
|
330
|
+
replace(pollute(delimiter))
|
331
|
+
end
|
327
332
|
|
328
|
-
|
329
|
-
|
330
|
-
|
333
|
+
def pop
|
334
|
+
self[-1]
|
335
|
+
end
|
331
336
|
|
332
|
-
|
333
|
-
|
334
|
-
|
337
|
+
def non_possessive
|
338
|
+
dup.non_possessive!
|
339
|
+
end
|
335
340
|
|
336
|
-
|
337
|
-
|
338
|
-
end
|
341
|
+
def non_possessive!
|
342
|
+
return self unless possessive?
|
339
343
|
|
340
|
-
|
341
|
-
|
344
|
+
chomp!("'s") || chomp!("'") || self
|
345
|
+
end
|
342
346
|
|
343
|
-
|
344
|
-
|
347
|
+
def possessive
|
348
|
+
return self if possessive?
|
345
349
|
|
346
|
-
|
347
|
-
|
350
|
+
possession = end_with?('s') ? "'" : "'s"
|
351
|
+
"#{self}#{possession}"
|
352
|
+
end
|
348
353
|
|
349
|
-
|
350
|
-
|
351
|
-
|
354
|
+
def possessive!
|
355
|
+
replace(possessive)
|
356
|
+
end
|
352
357
|
|
353
|
-
|
354
|
-
|
355
|
-
|
358
|
+
def possessive?
|
359
|
+
%w['s s'].any? { |pos| end_with?(pos) }
|
360
|
+
end
|
356
361
|
|
357
|
-
|
358
|
-
|
359
|
-
|
362
|
+
def push(string)
|
363
|
+
replace(concat(string))
|
364
|
+
end
|
360
365
|
|
361
|
-
|
362
|
-
|
363
|
-
|
366
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
367
|
+
def quote(type = :double, amount = nil)
|
368
|
+
if type.is_a?(Integer)
|
369
|
+
tmp = amount
|
370
|
+
amount = type
|
371
|
+
type = tmp || :mixed
|
372
|
+
else
|
373
|
+
amount ||= 1
|
374
|
+
end
|
364
375
|
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
c = (amount.to_f / 2).to_i
|
390
|
-
f = '"' * c
|
391
|
-
b = f
|
392
|
-
|
393
|
-
if amount.odd?
|
394
|
-
f = "'" + f
|
395
|
-
b += "'"
|
376
|
+
case type.to_s
|
377
|
+
when "'", 'single', 's', '1'
|
378
|
+
f = "'" * amount
|
379
|
+
b = f
|
380
|
+
when '"', 'double', 'd', '2'
|
381
|
+
f = '"' * amount
|
382
|
+
b = f
|
383
|
+
when '`', 'back', 'backtick', 'b', '-1'
|
384
|
+
f = '`' * amount
|
385
|
+
b = f
|
386
|
+
when "`'", 'bracket', 'sb'
|
387
|
+
f = '`' * amount
|
388
|
+
b = "'" * amount
|
389
|
+
when "'\"", 'mixed', 'm', 'Integer'
|
390
|
+
c = (amount.to_f / 2).to_i
|
391
|
+
f = '"' * c
|
392
|
+
b = f
|
393
|
+
|
394
|
+
if amount.odd?
|
395
|
+
f = "'" + f
|
396
|
+
b += "'"
|
397
|
+
end
|
398
|
+
else
|
399
|
+
raise ArgumentError, "Invalid quote type: #{type.inspect}"
|
396
400
|
end
|
397
|
-
else
|
398
|
-
raise ArgumentError, "Invalid quote type: #{type.inspect}"
|
399
|
-
end
|
400
401
|
|
401
|
-
|
402
|
-
|
403
|
-
|
402
|
+
"#{f}#{self}#{b}"
|
403
|
+
end
|
404
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
404
405
|
|
405
|
-
|
406
|
-
|
407
|
-
|
406
|
+
def quote!(type = :double, amount = nil)
|
407
|
+
replace(quote(type, amount))
|
408
|
+
end
|
408
409
|
|
409
|
-
|
410
|
-
|
411
|
-
|
410
|
+
def remove(*patterns)
|
411
|
+
dup.remove!(*patterns)
|
412
|
+
end
|
412
413
|
|
413
|
-
|
414
|
-
|
415
|
-
|
414
|
+
def remove!(*patterns)
|
415
|
+
patterns.each_with_object(self) do |pat, str|
|
416
|
+
pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '')
|
417
|
+
end
|
416
418
|
end
|
417
|
-
end
|
418
419
|
|
419
|
-
|
420
|
-
|
421
|
-
|
420
|
+
def remove_tags
|
421
|
+
dup.remove_tags!
|
422
|
+
end
|
422
423
|
|
423
|
-
|
424
|
-
|
425
|
-
|
424
|
+
def remove_tags!
|
425
|
+
gsub!(%r{<\/?[^>]*>}, '') || self
|
426
|
+
end
|
426
427
|
|
427
|
-
|
428
|
-
|
429
|
-
|
428
|
+
def rotate(amount = 1)
|
429
|
+
dup.rotate!(amount)
|
430
|
+
end
|
430
431
|
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
432
|
+
def rotate!(amount = 1)
|
433
|
+
amount += size if amount.negative?
|
434
|
+
slice!(amount, size - amount) + slice!(0, amount)
|
435
|
+
end
|
435
436
|
|
436
|
-
|
437
|
-
|
438
|
-
|
437
|
+
def sample(separator = ' ')
|
438
|
+
split(separator).sample
|
439
|
+
end
|
439
440
|
|
440
|
-
|
441
|
-
|
442
|
-
|
441
|
+
def sample!(separator = ' ')
|
442
|
+
replace(sample(separator))
|
443
|
+
end
|
443
444
|
|
444
|
-
|
445
|
-
|
446
|
-
|
445
|
+
def shift(*patterns)
|
446
|
+
dup.shift!(*patterns)
|
447
|
+
end
|
447
448
|
|
448
|
-
|
449
|
-
|
449
|
+
def shift!(*patterns)
|
450
|
+
return self[0] if patterns.empty?
|
450
451
|
|
451
|
-
|
452
|
-
|
452
|
+
patterns.each_with_object(self) { |pat, str| str.sub!(pat, '') }
|
453
|
+
end
|
453
454
|
|
454
|
-
|
455
|
-
|
456
|
-
|
455
|
+
def shuffle(separator = '')
|
456
|
+
split(separator).shuffle.join
|
457
|
+
end
|
457
458
|
|
458
|
-
|
459
|
-
|
460
|
-
|
459
|
+
def shuffle!(separator = '')
|
460
|
+
replace(shuffle(separator))
|
461
|
+
end
|
461
462
|
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
463
|
+
def sift(keep)
|
464
|
+
keep = case keep
|
465
|
+
when String then keep.chars
|
466
|
+
when Array then keep.map(&:to_s)
|
467
|
+
when Range then keep.to_a.map(&:to_s)
|
468
|
+
else raise TypeError, "Invalid parameter: #{keep.inspect}"
|
469
|
+
end
|
469
470
|
|
470
|
-
|
471
|
-
|
471
|
+
chars.keep_if { |chr| keep.include?(chr) }.join
|
472
|
+
end
|
472
473
|
|
473
|
-
|
474
|
-
|
475
|
-
|
474
|
+
def sift!(keep)
|
475
|
+
replace(sift(keep))
|
476
|
+
end
|
476
477
|
|
477
|
-
|
478
|
-
|
479
|
-
|
478
|
+
def slugify
|
479
|
+
dup.slugify!
|
480
|
+
end
|
480
481
|
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
482
|
+
def slugify!
|
483
|
+
gsub!(/[^\x00-\x7F]+/, '')
|
484
|
+
gsub!(/[^\w_ \-]+/i, '')
|
485
|
+
gsub!(/[ \-]+/i, '-')
|
486
|
+
gsub!(/^\-|\-$/i, '')
|
487
|
+
downcase! || self
|
488
|
+
end
|
488
489
|
|
489
|
-
|
490
|
-
|
491
|
-
|
490
|
+
def squish
|
491
|
+
dup.squish!
|
492
|
+
end
|
492
493
|
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
494
|
+
def squish!
|
495
|
+
strip!
|
496
|
+
gsub!(/\s+/, ' ') || self
|
497
|
+
end
|
497
498
|
|
498
|
-
|
499
|
-
|
500
|
-
|
499
|
+
def sort
|
500
|
+
chars.sort.join
|
501
|
+
end
|
501
502
|
|
502
|
-
|
503
|
-
|
504
|
-
|
503
|
+
def sort!
|
504
|
+
replace(sort)
|
505
|
+
end
|
505
506
|
|
506
|
-
|
507
|
-
|
508
|
-
|
507
|
+
def titleize
|
508
|
+
dup.titleize!
|
509
|
+
end
|
509
510
|
|
510
|
-
|
511
|
+
alias titlecase titleize
|
511
512
|
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
513
|
+
def titleize!
|
514
|
+
underscore!
|
515
|
+
humanize!
|
516
|
+
gsub!(/\b(?<!['’`])[a-z]/) { $&.capitalize } || self
|
517
|
+
end
|
517
518
|
|
518
|
-
|
519
|
+
alias titlecase! titleize!
|
519
520
|
|
520
|
-
|
521
|
-
|
522
|
-
|
521
|
+
def to(position)
|
522
|
+
self[0..position]
|
523
|
+
end
|
523
524
|
|
524
|
-
|
525
|
-
|
526
|
-
|
525
|
+
def transliterize
|
526
|
+
dup.transliterize!
|
527
|
+
end
|
527
528
|
|
528
|
-
|
529
|
-
|
530
|
-
|
529
|
+
def transliterize!
|
530
|
+
TRANSLITERATIONS.each_with_object(self) { |(k, v), str| str.gsub!(k, v) }
|
531
|
+
end
|
531
532
|
|
532
|
-
|
533
|
-
|
533
|
+
def truncate(truncate_at, options = {})
|
534
|
+
return dup unless length > truncate_at
|
534
535
|
|
535
|
-
|
536
|
-
|
537
|
-
|
536
|
+
seperator = options[:separator]
|
537
|
+
omission = options[:omission] || '...'
|
538
|
+
size_with_room_for_omission = truncate_at - omission.length
|
538
539
|
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
540
|
+
stop = if seperator
|
541
|
+
rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
|
542
|
+
else
|
543
|
+
size_with_room_for_omission
|
544
|
+
end
|
544
545
|
|
545
|
-
|
546
|
-
|
546
|
+
"#{self[0, stop]}#{omission}"
|
547
|
+
end
|
547
548
|
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
549
|
+
def truncate_words(words_count, options = {})
|
550
|
+
sep = options[:separator] || /\s+/
|
551
|
+
sep = Regexp.escape(sep.to_s) unless sep.is_a(Regexp)
|
552
|
+
return self unless /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m.match?(self)
|
552
553
|
|
553
|
-
|
554
|
-
|
554
|
+
"#{::Regexp.last_match(1)}#{options[:omissio] || '...'}"
|
555
|
+
end
|
555
556
|
|
556
|
-
|
557
|
-
|
558
|
-
|
557
|
+
def underscore
|
558
|
+
dup.underscore!
|
559
|
+
end
|
559
560
|
|
560
|
-
|
561
|
+
alias snakecase underscore
|
561
562
|
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
563
|
+
def underscore!
|
564
|
+
camelize!
|
565
|
+
gsub!(/::/, '/')
|
566
|
+
gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
|
567
|
+
gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
568
|
+
tr!('-', '_')
|
569
|
+
downcase! || self
|
570
|
+
end
|
570
571
|
|
571
|
-
|
572
|
+
alias snakecase! underscore!
|
572
573
|
|
573
|
-
|
574
|
-
|
575
|
-
|
574
|
+
def unpollute(delimiter = '^--^--^')
|
575
|
+
dup.unpollute!(delimiter)
|
576
|
+
end
|
576
577
|
|
577
|
-
|
578
|
-
|
579
|
-
|
578
|
+
def unpollute!(delimiter = '^--^--^')
|
579
|
+
gsub!(delimiter, '') || self
|
580
|
+
end
|
580
581
|
|
581
|
-
|
582
|
-
|
583
|
-
|
582
|
+
def upcase?
|
583
|
+
upcase == self
|
584
|
+
end
|
584
585
|
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
586
|
+
def unshift(*patterns)
|
587
|
+
patterns.each_with_object('') { |pat, str| str << pat }
|
588
|
+
.concat(self)
|
589
|
+
end
|
589
590
|
|
590
|
-
|
591
|
-
|
592
|
-
|
591
|
+
def unshift!(*patterns)
|
592
|
+
replace(unshift(*patterns))
|
593
|
+
end
|
593
594
|
|
594
|
-
|
595
|
-
|
596
|
-
|
595
|
+
def unquote
|
596
|
+
dup.unquote!
|
597
|
+
end
|
597
598
|
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
599
|
+
def unquote!
|
600
|
+
[0, -1].each do |i|
|
601
|
+
case self[i, 1]
|
602
|
+
when "'", '"', '`' then self[i] = ''
|
603
|
+
end
|
602
604
|
end
|
603
|
-
end
|
604
605
|
|
605
|
-
|
606
|
-
|
606
|
+
self
|
607
|
+
end
|
607
608
|
|
608
|
-
|
609
|
-
|
610
|
-
|
609
|
+
def words
|
610
|
+
split(/\s+/)
|
611
|
+
end
|
611
612
|
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
613
|
+
def words_without_punctuation
|
614
|
+
str = dup
|
615
|
+
str.gsub!(%r{[.?¿¡…!,::;—"。?!、‘“”„«»〈〉《》,/\[\]]}, ' ')
|
616
|
+
str.gsub!('- ', ' ')
|
617
|
+
str.squeeze!(' ')
|
618
|
+
str.strip!
|
619
|
+
str.words
|
620
|
+
end
|
620
621
|
|
621
|
-
|
622
|
-
|
623
|
-
|
622
|
+
def variablize
|
623
|
+
"@#{gsub(/\W/, '_')}"
|
624
|
+
end
|
624
625
|
|
625
|
-
|
626
|
-
|
627
|
-
|
626
|
+
def variablize!
|
627
|
+
replace(variablize)
|
628
|
+
end
|
628
629
|
|
629
|
-
|
630
|
-
|
630
|
+
alias ends_with? end_with?
|
631
|
+
alias starts_with? start_with?
|
631
632
|
|
633
|
+
end
|
632
634
|
end
|