code-ruby 3.1.2 → 4.0.1
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.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/bin/code +100 -20
- data/lib/code/concerns/shared.rb +335 -15
- data/lib/code/format.rb +33 -15
- data/lib/code/network.rb +82 -0
- data/lib/code/node/call.rb +80 -2
- data/lib/code/node/call_argument.rb +14 -0
- data/lib/code/node/code.rb +4 -3
- data/lib/code/node/function_parameter.rb +7 -4
- data/lib/code/node/list.rb +32 -2
- data/lib/code/node/square_bracket.rb +4 -2
- data/lib/code/object/base_64.rb +132 -6
- data/lib/code/object/boolean.rb +56 -0
- data/lib/code/object/class.rb +143 -2
- data/lib/code/object/code.rb +108 -7
- data/lib/code/object/context.rb +59 -1
- data/lib/code/object/cryptography.rb +69 -0
- data/lib/code/object/date.rb +13800 -462
- data/lib/code/object/decimal.rb +1098 -0
- data/lib/code/object/dictionary.rb +1861 -11
- data/lib/code/object/duration.rb +24 -0
- data/lib/code/object/function.rb +289 -27
- data/lib/code/object/global.rb +447 -1
- data/lib/code/object/html.rb +181 -7
- data/lib/code/object/http.rb +253 -17
- data/lib/code/object/ics.rb +76 -13
- data/lib/code/object/identifier_list.rb +30 -10
- data/lib/code/object/integer.rb +1265 -2
- data/lib/code/object/json.rb +80 -1
- data/lib/code/object/list.rb +3371 -10
- data/lib/code/object/nothing.rb +53 -0
- data/lib/code/object/number.rb +120 -0
- data/lib/code/object/parameter.rb +149 -0
- data/lib/code/object/range.rb +530 -14
- data/lib/code/object/smtp.rb +103 -12
- data/lib/code/object/string.rb +968 -3
- data/lib/code/object/super.rb +11 -1
- data/lib/code/object/time.rb +13932 -498
- data/lib/code/object/url.rb +67 -0
- data/lib/code/object.rb +582 -0
- data/lib/code/parser.rb +194 -55
- data/lib/code-ruby.rb +3 -0
- data/lib/code.rb +30 -3
- metadata +135 -84
- data/.github/dependabot.yml +0 -15
- data/.github/workflows/ci.yml +0 -38
- data/.gitignore +0 -30
- data/.node-version +0 -1
- data/.npm-version +0 -1
- data/.prettierignore +0 -2
- data/.rspec +0 -1
- data/.rubocop.yml +0 -140
- data/.ruby-version +0 -1
- data/.tool-versions +0 -3
- data/AGENTS.md +0 -43
- data/Gemfile +0 -22
- data/Gemfile.lock +0 -292
- data/Rakefile +0 -5
- data/bin/bundle +0 -123
- data/bin/bundle-audit +0 -31
- data/bin/bundler-audit +0 -31
- data/bin/dorian +0 -31
- data/bin/rspec +0 -31
- data/bin/rubocop +0 -31
- data/bin/test +0 -5
- data/code-ruby.gemspec +0 -34
- data/docs/precedence.txt +0 -36
- data/package-lock.json +0 -14
- data/package.json +0 -7
- data/spec/bin/code_spec.rb +0 -48
- data/spec/code/format_spec.rb +0 -153
- data/spec/code/node/call_spec.rb +0 -11
- data/spec/code/object/boolean_spec.rb +0 -18
- data/spec/code/object/cryptography_spec.rb +0 -25
- data/spec/code/object/decimal_spec.rb +0 -50
- data/spec/code/object/dictionary_spec.rb +0 -98
- data/spec/code/object/function_spec.rb +0 -268
- data/spec/code/object/http_spec.rb +0 -33
- data/spec/code/object/ics_spec.rb +0 -50
- data/spec/code/object/integer_spec.rb +0 -42
- data/spec/code/object/list_spec.rb +0 -22
- data/spec/code/object/nothing_spec.rb +0 -14
- data/spec/code/object/range_spec.rb +0 -23
- data/spec/code/object/string_spec.rb +0 -26
- data/spec/code/parser/boolean_spec.rb +0 -11
- data/spec/code/parser/chained_call_spec.rb +0 -16
- data/spec/code/parser/dictionary_spec.rb +0 -18
- data/spec/code/parser/function_spec.rb +0 -16
- data/spec/code/parser/group_spec.rb +0 -11
- data/spec/code/parser/if_modifier_spec.rb +0 -18
- data/spec/code/parser/list_spec.rb +0 -17
- data/spec/code/parser/number_spec.rb +0 -11
- data/spec/code/parser/string_spec.rb +0 -20
- data/spec/code/parser_spec.rb +0 -52
- data/spec/code/type_spec.rb +0 -21
- data/spec/code_spec.rb +0 -717
- data/spec/spec_helper.rb +0 -21
- data/spec/zeitwerk/loader_spec.rb +0 -7
data/lib/code/object/string.rb
CHANGED
|
@@ -3,6 +3,546 @@
|
|
|
3
3
|
class Code
|
|
4
4
|
class Object
|
|
5
5
|
class String < Object
|
|
6
|
+
CLASS_DOCUMENTATION = {
|
|
7
|
+
name: "String",
|
|
8
|
+
description:
|
|
9
|
+
"represents text and provides parsing, search, and transformation operations.",
|
|
10
|
+
examples: ["\"hello\"", ":hello.upcase", "\"a,b\".split(\",\")"]
|
|
11
|
+
}.freeze
|
|
12
|
+
INSTANCE_FUNCTIONS = {
|
|
13
|
+
"&" => {
|
|
14
|
+
name: "&",
|
|
15
|
+
description: "returns a function that calls the named selector.",
|
|
16
|
+
examples: ["&\"to_string\"", "&\"upcase\"", "&\"value\""]
|
|
17
|
+
},
|
|
18
|
+
"to_function" => {
|
|
19
|
+
name: "to_function",
|
|
20
|
+
description: "returns a function that calls the named selector.",
|
|
21
|
+
examples: [
|
|
22
|
+
"\"to_string\".to_function",
|
|
23
|
+
"\"upcase\".to_function",
|
|
24
|
+
"\"value\".to_function"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
"*" => {
|
|
28
|
+
name: "*",
|
|
29
|
+
description: "returns the string repeated a number of times.",
|
|
30
|
+
examples: ["\"a\" * 3", "\":\" * 2", "\"ab\" * 2"]
|
|
31
|
+
},
|
|
32
|
+
"+" => {
|
|
33
|
+
name: "+",
|
|
34
|
+
description: "returns the string with another value appended.",
|
|
35
|
+
examples: ["\"a\" + \"b\"", "\"count: \" + 1", "\":\" + :ok"]
|
|
36
|
+
},
|
|
37
|
+
"downcase" => {
|
|
38
|
+
name: "downcase",
|
|
39
|
+
description: "returns the string converted to lowercase.",
|
|
40
|
+
examples: [
|
|
41
|
+
"\"HELLO\".downcase",
|
|
42
|
+
"\"Code\".downcase",
|
|
43
|
+
"\"A1\".downcase"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"lower_case" => {
|
|
47
|
+
name: "lower_case",
|
|
48
|
+
description: "returns the string converted to lowercase.",
|
|
49
|
+
examples: [
|
|
50
|
+
"\"HELLO\".lower_case",
|
|
51
|
+
"\"Code\".lower_case",
|
|
52
|
+
"\"A1\".lower_case"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
"include?" => {
|
|
56
|
+
name: "include?",
|
|
57
|
+
description: "returns whether the string includes another string.",
|
|
58
|
+
examples: [
|
|
59
|
+
"\"hello\".include?(\"ell\")",
|
|
60
|
+
"\"hello\".include?(\"x\")",
|
|
61
|
+
"\":name\".include?(\":\")"
|
|
62
|
+
]
|
|
63
|
+
},
|
|
64
|
+
"member?" => {
|
|
65
|
+
name: "member?",
|
|
66
|
+
description: "returns whether the string includes another string.",
|
|
67
|
+
examples: [
|
|
68
|
+
"\"hello\".member?(\"ell\")",
|
|
69
|
+
"\"hello\".member?(\"x\")",
|
|
70
|
+
"\":name\".member?(\":\")"
|
|
71
|
+
]
|
|
72
|
+
},
|
|
73
|
+
"starts_with?" => {
|
|
74
|
+
name: "starts_with?",
|
|
75
|
+
description: "returns whether the string starts with another string.",
|
|
76
|
+
examples: [
|
|
77
|
+
"\"hello\".starts_with?(\"he\")",
|
|
78
|
+
"\"hello\".starts_with?(\"lo\")",
|
|
79
|
+
"\":name\".starts_with?(\":\")"
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
"start_with?" => {
|
|
83
|
+
name: "start_with?",
|
|
84
|
+
description: "returns whether the string starts with another string.",
|
|
85
|
+
examples: [
|
|
86
|
+
"\"hello\".start_with?(\"he\")",
|
|
87
|
+
"\"hello\".start_with?(\"lo\")",
|
|
88
|
+
"\":name\".start_with?(\":\")"
|
|
89
|
+
]
|
|
90
|
+
},
|
|
91
|
+
"ends_with?" => {
|
|
92
|
+
name: "ends_with?",
|
|
93
|
+
description: "returns whether the string ends with another string.",
|
|
94
|
+
examples: [
|
|
95
|
+
"\"hello\".ends_with?(\"lo\")",
|
|
96
|
+
"\"hello\".ends_with?(\"he\")",
|
|
97
|
+
"\"file.rb\".ends_with?(\".rb\")"
|
|
98
|
+
]
|
|
99
|
+
},
|
|
100
|
+
"end_with?" => {
|
|
101
|
+
name: "end_with?",
|
|
102
|
+
description: "returns whether the string ends with another string.",
|
|
103
|
+
examples: [
|
|
104
|
+
"\"hello\".end_with?(\"lo\")",
|
|
105
|
+
"\"hello\".end_with?(\"he\")",
|
|
106
|
+
"\"file.rb\".end_with?(\".rb\")"
|
|
107
|
+
]
|
|
108
|
+
},
|
|
109
|
+
"[]" => {
|
|
110
|
+
name: "[]",
|
|
111
|
+
description: "returns the character at an index.",
|
|
112
|
+
examples: ["\"abc\"[0]", "\"abc\"[1]", "\"abc\"[2]"]
|
|
113
|
+
},
|
|
114
|
+
"at" => {
|
|
115
|
+
name: "at",
|
|
116
|
+
description: "returns the character at an index.",
|
|
117
|
+
examples: ["\"abc\".at(0)", "\"abc\".at(1)", "\"abc\".at(2)"]
|
|
118
|
+
},
|
|
119
|
+
"get" => {
|
|
120
|
+
name: "get",
|
|
121
|
+
description: "returns the character at an index.",
|
|
122
|
+
examples: ["\"abc\".get(0)", "\"abc\".get(1)", "\"abc\".get(2)"]
|
|
123
|
+
},
|
|
124
|
+
"capitalize" => {
|
|
125
|
+
name: "capitalize",
|
|
126
|
+
description:
|
|
127
|
+
"returns the string with its first character capitalized.",
|
|
128
|
+
examples: [
|
|
129
|
+
"\"hello\".capitalize",
|
|
130
|
+
"\"code\".capitalize",
|
|
131
|
+
"\"already\".capitalize"
|
|
132
|
+
]
|
|
133
|
+
},
|
|
134
|
+
"characters" => {
|
|
135
|
+
name: "characters",
|
|
136
|
+
description: "returns the string characters as a list.",
|
|
137
|
+
examples: [
|
|
138
|
+
"\"abc\".characters",
|
|
139
|
+
"\"hi\".characters",
|
|
140
|
+
"\"\".characters"
|
|
141
|
+
]
|
|
142
|
+
},
|
|
143
|
+
"bytes" => {
|
|
144
|
+
name: "bytes",
|
|
145
|
+
description: "returns the string bytes as a list.",
|
|
146
|
+
examples: ["\"abc\".bytes", "\"A\".bytes", "\"\".bytes"]
|
|
147
|
+
},
|
|
148
|
+
"bytesize" => {
|
|
149
|
+
name: "bytesize",
|
|
150
|
+
description: "returns the number of bytes in the string.",
|
|
151
|
+
examples: ["\"abc\".bytesize", "\"A\".bytesize", "\"\".bytesize"]
|
|
152
|
+
},
|
|
153
|
+
"byte_slice" => {
|
|
154
|
+
name: "byte_slice",
|
|
155
|
+
description: "returns a byte slice from the string.",
|
|
156
|
+
examples: [
|
|
157
|
+
"\"abc\".byte_slice(0)",
|
|
158
|
+
"\"abc\".byte_slice(0, 2)",
|
|
159
|
+
"\"hello\".byte_slice(1, 3)"
|
|
160
|
+
]
|
|
161
|
+
},
|
|
162
|
+
"codepoints" => {
|
|
163
|
+
name: "codepoints",
|
|
164
|
+
description: "returns the string codepoints as a list.",
|
|
165
|
+
examples: [
|
|
166
|
+
"\"abc\".codepoints",
|
|
167
|
+
"\"A\".codepoints",
|
|
168
|
+
"\"\".codepoints"
|
|
169
|
+
]
|
|
170
|
+
},
|
|
171
|
+
"character_code_at" => {
|
|
172
|
+
name: "character_code_at",
|
|
173
|
+
description: "returns the codepoint at an index.",
|
|
174
|
+
examples: [
|
|
175
|
+
"\"abc\".character_code_at(0)",
|
|
176
|
+
"\"abc\".character_code_at(1)",
|
|
177
|
+
"\"abc\".character_code_at"
|
|
178
|
+
]
|
|
179
|
+
},
|
|
180
|
+
"ordinal" => {
|
|
181
|
+
name: "ordinal",
|
|
182
|
+
description: "returns the codepoint for the first character.",
|
|
183
|
+
examples: ["\"a\".ordinal", "\"A\".ordinal", "\"1\".ordinal"]
|
|
184
|
+
},
|
|
185
|
+
"chomp" => {
|
|
186
|
+
name: "chomp",
|
|
187
|
+
description:
|
|
188
|
+
"returns the string with a trailing record separator removed.",
|
|
189
|
+
examples: ["\"a\\n\".chomp", "\"a\".chomp", "\"a\\r\\n\".chomp"]
|
|
190
|
+
},
|
|
191
|
+
"chop" => {
|
|
192
|
+
name: "chop",
|
|
193
|
+
description: "returns the string with its last character removed.",
|
|
194
|
+
examples: ["\"abc\".chop", "\"a\".chop", "\"\".chop"]
|
|
195
|
+
},
|
|
196
|
+
"delete" => {
|
|
197
|
+
name: "delete",
|
|
198
|
+
description: "returns the string with matching characters removed.",
|
|
199
|
+
examples: [
|
|
200
|
+
"\"hello\".delete(\"l\")",
|
|
201
|
+
"\"123\".delete(\"2\")",
|
|
202
|
+
"\"abc\".delete(\"z\")"
|
|
203
|
+
]
|
|
204
|
+
},
|
|
205
|
+
"delete_prefix" => {
|
|
206
|
+
name: "delete_prefix",
|
|
207
|
+
description: "returns the string with a matching prefix removed.",
|
|
208
|
+
examples: [
|
|
209
|
+
"\"hello\".delete_prefix(\"he\")",
|
|
210
|
+
"\"hello\".delete_prefix(\"x\")",
|
|
211
|
+
"\"file.rb\".delete_prefix(\"file\")"
|
|
212
|
+
]
|
|
213
|
+
},
|
|
214
|
+
"delete_suffix" => {
|
|
215
|
+
name: "delete_suffix",
|
|
216
|
+
description: "returns the string with a matching suffix removed.",
|
|
217
|
+
examples: [
|
|
218
|
+
"\"hello\".delete_suffix(\"lo\")",
|
|
219
|
+
"\"hello\".delete_suffix(\"x\")",
|
|
220
|
+
"\"file.rb\".delete_suffix(\".rb\")"
|
|
221
|
+
]
|
|
222
|
+
},
|
|
223
|
+
"empty?" => {
|
|
224
|
+
name: "empty?",
|
|
225
|
+
description: "returns whether the string is empty.",
|
|
226
|
+
examples: ["\"\".empty?", "\"a\".empty?", "\" \".empty?"]
|
|
227
|
+
},
|
|
228
|
+
"clear" => {
|
|
229
|
+
name: "clear",
|
|
230
|
+
description: "empties the string and returns it.",
|
|
231
|
+
examples: ["\"abc\".clear", "\"a\".clear", "\"\".clear"]
|
|
232
|
+
},
|
|
233
|
+
"count" => {
|
|
234
|
+
name: "count",
|
|
235
|
+
description: "returns the count of matching characters.",
|
|
236
|
+
examples: [
|
|
237
|
+
"\"hello\".count(\"l\")",
|
|
238
|
+
"\"banana\".count(\"a\")",
|
|
239
|
+
"\"abc\".count(\"z\")"
|
|
240
|
+
]
|
|
241
|
+
},
|
|
242
|
+
"insert" => {
|
|
243
|
+
name: "insert",
|
|
244
|
+
description: "inserts a value at an index and returns the string.",
|
|
245
|
+
examples: [
|
|
246
|
+
"\"ac\".insert(1, \"b\")",
|
|
247
|
+
"\"bc\".insert(0, \"a\")",
|
|
248
|
+
"\"ab\".insert(2, \"c\")"
|
|
249
|
+
]
|
|
250
|
+
},
|
|
251
|
+
"prepend" => {
|
|
252
|
+
name: "prepend",
|
|
253
|
+
description: "prepends a value and returns the string.",
|
|
254
|
+
examples: [
|
|
255
|
+
"\"b\".prepend(\"a\")",
|
|
256
|
+
"\"world\".prepend(\"hello \")",
|
|
257
|
+
"\"1\".prepend(0)"
|
|
258
|
+
]
|
|
259
|
+
},
|
|
260
|
+
"concat" => {
|
|
261
|
+
name: "concat",
|
|
262
|
+
description: "appends values and returns the string.",
|
|
263
|
+
examples: [
|
|
264
|
+
"\"a\".concat(\"b\")",
|
|
265
|
+
"\"a\".concat(\"b\", \"c\")",
|
|
266
|
+
"\"count\".concat(1)"
|
|
267
|
+
]
|
|
268
|
+
},
|
|
269
|
+
"first" => {
|
|
270
|
+
name: "first",
|
|
271
|
+
description: "returns the first character or first characters.",
|
|
272
|
+
examples: ["\"abc\".first", "\"abc\".first(2)", "\"\".first"]
|
|
273
|
+
},
|
|
274
|
+
"index" => {
|
|
275
|
+
name: "index",
|
|
276
|
+
description: "returns the index of a matching substring.",
|
|
277
|
+
examples: [
|
|
278
|
+
"\"hello\".index(\"l\")",
|
|
279
|
+
"\"hello\".index(\"x\")",
|
|
280
|
+
"\"banana\".index(\"na\")"
|
|
281
|
+
]
|
|
282
|
+
},
|
|
283
|
+
"last" => {
|
|
284
|
+
name: "last",
|
|
285
|
+
description: "returns the last character or last characters.",
|
|
286
|
+
examples: ["\"abc\".last", "\"ab\".last", "\"\".last"]
|
|
287
|
+
},
|
|
288
|
+
"lines" => {
|
|
289
|
+
name: "lines",
|
|
290
|
+
description: "returns the string lines as a list.",
|
|
291
|
+
examples: ["\"a\\nb\".lines", "\"a\".lines", "\"\".lines"]
|
|
292
|
+
},
|
|
293
|
+
"reverse" => {
|
|
294
|
+
name: "reverse",
|
|
295
|
+
description: "returns the string with characters reversed.",
|
|
296
|
+
examples: ["\"abc\".reverse", "\"ab\".reverse", "\"\".reverse"]
|
|
297
|
+
},
|
|
298
|
+
"right_index" => {
|
|
299
|
+
name: "right_index",
|
|
300
|
+
description: "returns the last index of a matching substring.",
|
|
301
|
+
examples: [
|
|
302
|
+
"\"hello\".right_index(\"l\")",
|
|
303
|
+
"\"hello\".right_index(\"x\")",
|
|
304
|
+
"\"banana\".right_index(\"na\")"
|
|
305
|
+
]
|
|
306
|
+
},
|
|
307
|
+
"parameterize" => {
|
|
308
|
+
name: "parameterize",
|
|
309
|
+
description:
|
|
310
|
+
"returns the string parameterized for identifiers or urls.",
|
|
311
|
+
examples: [
|
|
312
|
+
"\"Hello world\".parameterize",
|
|
313
|
+
"\"a b c\".parameterize",
|
|
314
|
+
"\"Code Ruby\".parameterize"
|
|
315
|
+
]
|
|
316
|
+
},
|
|
317
|
+
"squish" => {
|
|
318
|
+
name: "squish",
|
|
319
|
+
description:
|
|
320
|
+
"returns the string with surrounding and repeated whitespace collapsed.",
|
|
321
|
+
examples: [
|
|
322
|
+
"\" hello world \".squish",
|
|
323
|
+
"\"a\\n b\".squish",
|
|
324
|
+
"\"a b\".squish"
|
|
325
|
+
]
|
|
326
|
+
},
|
|
327
|
+
"squeeze" => {
|
|
328
|
+
name: "squeeze",
|
|
329
|
+
description: "returns the string with repeated characters collapsed.",
|
|
330
|
+
examples: [
|
|
331
|
+
"\"hellooo\".squeeze",
|
|
332
|
+
"\"book\".squeeze(\"o\")",
|
|
333
|
+
"\"aaab\".squeeze"
|
|
334
|
+
]
|
|
335
|
+
},
|
|
336
|
+
"substitute" => {
|
|
337
|
+
name: "substitute",
|
|
338
|
+
description: "returns the string with the first match replaced.",
|
|
339
|
+
examples: [
|
|
340
|
+
"\"hello\".substitute(\"l\", \"x\")",
|
|
341
|
+
"\"abc\".substitute(\"a\", \"z\")",
|
|
342
|
+
"\"abc\".substitute(\"x\", \"z\")"
|
|
343
|
+
]
|
|
344
|
+
},
|
|
345
|
+
"substitute!" => {
|
|
346
|
+
name: "substitute!",
|
|
347
|
+
description: "replaces the first match in the string and returns it.",
|
|
348
|
+
examples: [
|
|
349
|
+
"\"hello\".substitute!(\"l\", \"x\")",
|
|
350
|
+
"\"abc\".substitute!(\"a\", \"z\")",
|
|
351
|
+
"\"abc\".substitute!(\"x\", \"z\")"
|
|
352
|
+
]
|
|
353
|
+
},
|
|
354
|
+
"substitute_all" => {
|
|
355
|
+
name: "substitute_all",
|
|
356
|
+
description: "returns the string with all matches replaced.",
|
|
357
|
+
examples: [
|
|
358
|
+
"\"hello\".substitute_all(\"l\", \"x\")",
|
|
359
|
+
"\"abcabc\".substitute_all(\"a\", \"z\")",
|
|
360
|
+
"\"abc\".substitute_all(\"x\", \"z\")"
|
|
361
|
+
]
|
|
362
|
+
},
|
|
363
|
+
"substitute_all!" => {
|
|
364
|
+
name: "substitute_all!",
|
|
365
|
+
description: "replaces all matches in the string and returns it.",
|
|
366
|
+
examples: [
|
|
367
|
+
"\"hello\".substitute_all!(\"l\", \"x\")",
|
|
368
|
+
"\"abcabc\".substitute_all!(\"a\", \"z\")",
|
|
369
|
+
"\"abc\".substitute_all!(\"x\", \"z\")"
|
|
370
|
+
]
|
|
371
|
+
},
|
|
372
|
+
"substitute_once" => {
|
|
373
|
+
name: "substitute_once",
|
|
374
|
+
description: "returns the string with the first match replaced.",
|
|
375
|
+
examples: [
|
|
376
|
+
"\"hello\".substitute_once(\"l\", \"x\")",
|
|
377
|
+
"\"abc\".substitute_once(\"a\", \"z\")",
|
|
378
|
+
"\"abc\".substitute_once(\"x\", \"z\")"
|
|
379
|
+
]
|
|
380
|
+
},
|
|
381
|
+
"substitute_once!" => {
|
|
382
|
+
name: "substitute_once!",
|
|
383
|
+
description: "replaces the first match in the string and returns it.",
|
|
384
|
+
examples: [
|
|
385
|
+
"\"hello\".substitute_once!(\"l\", \"x\")",
|
|
386
|
+
"\"abc\".substitute_once!(\"a\", \"z\")",
|
|
387
|
+
"\"abc\".substitute_once!(\"x\", \"z\")"
|
|
388
|
+
]
|
|
389
|
+
},
|
|
390
|
+
"swapcase" => {
|
|
391
|
+
name: "swapcase",
|
|
392
|
+
description: "returns the string with letter case swapped.",
|
|
393
|
+
examples: [
|
|
394
|
+
"\"AbC\".swapcase",
|
|
395
|
+
"\"hello\".swapcase",
|
|
396
|
+
"\"ABC\".swapcase"
|
|
397
|
+
]
|
|
398
|
+
},
|
|
399
|
+
"titleize" => {
|
|
400
|
+
name: "titleize",
|
|
401
|
+
description: "returns the string converted to title case.",
|
|
402
|
+
examples: [
|
|
403
|
+
"\"hello world\".titleize",
|
|
404
|
+
"\"code ruby\".titleize",
|
|
405
|
+
"\"one\".titleize"
|
|
406
|
+
]
|
|
407
|
+
},
|
|
408
|
+
"upcase" => {
|
|
409
|
+
name: "upcase",
|
|
410
|
+
description: "returns the string converted to uppercase.",
|
|
411
|
+
examples: ["\"hello\".upcase", "\"Code\".upcase", "\"a1\".upcase"]
|
|
412
|
+
},
|
|
413
|
+
"upper_case" => {
|
|
414
|
+
name: "upper_case",
|
|
415
|
+
description: "returns the string converted to uppercase.",
|
|
416
|
+
examples: [
|
|
417
|
+
"\"hello\".upper_case",
|
|
418
|
+
"\"Code\".upper_case",
|
|
419
|
+
"\"a1\".upper_case"
|
|
420
|
+
]
|
|
421
|
+
},
|
|
422
|
+
"size" => {
|
|
423
|
+
name: "size",
|
|
424
|
+
description: "returns the number of characters in the string.",
|
|
425
|
+
examples: ["\"abc\".size", "\"\".size", "\"hello\".size"]
|
|
426
|
+
},
|
|
427
|
+
"length" => {
|
|
428
|
+
name: "length",
|
|
429
|
+
description: "returns the number of characters in the string.",
|
|
430
|
+
examples: ["\"abc\".length", "\"\".length", "\"hello\".length"]
|
|
431
|
+
},
|
|
432
|
+
"strip" => {
|
|
433
|
+
name: "strip",
|
|
434
|
+
description:
|
|
435
|
+
"returns the string with surrounding whitespace removed.",
|
|
436
|
+
examples: ["\" a \".strip", "\"\\na\".strip", "\"a\".strip"]
|
|
437
|
+
},
|
|
438
|
+
"left_strip" => {
|
|
439
|
+
name: "left_strip",
|
|
440
|
+
description: "returns the string with leading whitespace removed.",
|
|
441
|
+
examples: [
|
|
442
|
+
"\" a\".left_strip",
|
|
443
|
+
"\"\\na\".left_strip",
|
|
444
|
+
"\"a\".left_strip"
|
|
445
|
+
]
|
|
446
|
+
},
|
|
447
|
+
"right_strip" => {
|
|
448
|
+
name: "right_strip",
|
|
449
|
+
description: "returns the string with trailing whitespace removed.",
|
|
450
|
+
examples: [
|
|
451
|
+
"\"a \".right_strip",
|
|
452
|
+
"\"a\\n\".right_strip",
|
|
453
|
+
"\"a\".right_strip"
|
|
454
|
+
]
|
|
455
|
+
},
|
|
456
|
+
"slice" => {
|
|
457
|
+
name: "slice",
|
|
458
|
+
description: "returns a slice from the string.",
|
|
459
|
+
examples: [
|
|
460
|
+
"\"abc\".slice(0)",
|
|
461
|
+
"\"abc\".slice(0, 2)",
|
|
462
|
+
"\"abc\".slice(1)"
|
|
463
|
+
]
|
|
464
|
+
},
|
|
465
|
+
"left_justify" => {
|
|
466
|
+
name: "left_justify",
|
|
467
|
+
description: "returns the string left-justified to a width.",
|
|
468
|
+
examples: [
|
|
469
|
+
"\"a\".left_justify(3)",
|
|
470
|
+
"\"a\".left_justify(3, \".\")",
|
|
471
|
+
"\"abc\".left_justify(2)"
|
|
472
|
+
]
|
|
473
|
+
},
|
|
474
|
+
"right_justify" => {
|
|
475
|
+
name: "right_justify",
|
|
476
|
+
description: "returns the string right-justified to a width.",
|
|
477
|
+
examples: [
|
|
478
|
+
"\"a\".right_justify(3)",
|
|
479
|
+
"\"a\".right_justify(3, \".\")",
|
|
480
|
+
"\"abc\".right_justify(2)"
|
|
481
|
+
]
|
|
482
|
+
},
|
|
483
|
+
"center" => {
|
|
484
|
+
name: "center",
|
|
485
|
+
description: "returns the string centered within a width.",
|
|
486
|
+
examples: [
|
|
487
|
+
"\"a\".center(3)",
|
|
488
|
+
"\"a\".center(3, \".\")",
|
|
489
|
+
"\"abc\".center(2)"
|
|
490
|
+
]
|
|
491
|
+
},
|
|
492
|
+
"pad_start" => {
|
|
493
|
+
name: "pad_start",
|
|
494
|
+
description: "returns the string padded at the beginning.",
|
|
495
|
+
examples: [
|
|
496
|
+
"\"a\".pad_start(3)",
|
|
497
|
+
"\"a\".pad_start(3, \"0\")",
|
|
498
|
+
"\"abc\".pad_start(2)"
|
|
499
|
+
]
|
|
500
|
+
},
|
|
501
|
+
"pad_end" => {
|
|
502
|
+
name: "pad_end",
|
|
503
|
+
description: "returns the string padded at the end.",
|
|
504
|
+
examples: [
|
|
505
|
+
"\"a\".pad_end(3)",
|
|
506
|
+
"\"a\".pad_end(3, \"0\")",
|
|
507
|
+
"\"abc\".pad_end(2)"
|
|
508
|
+
]
|
|
509
|
+
},
|
|
510
|
+
"repeat" => {
|
|
511
|
+
name: "repeat",
|
|
512
|
+
description: "returns the string repeated a number of times.",
|
|
513
|
+
examples: ["\"a\".repeat(3)", "\":\".repeat(2)", "\"ab\".repeat(2)"]
|
|
514
|
+
},
|
|
515
|
+
"substring" => {
|
|
516
|
+
name: "substring",
|
|
517
|
+
description: "returns a substring from the string.",
|
|
518
|
+
examples: [
|
|
519
|
+
"\"abc\".substring(0)",
|
|
520
|
+
"\"abc\".substring(0, 2)",
|
|
521
|
+
"\"hello\".substring(1, 3)"
|
|
522
|
+
]
|
|
523
|
+
},
|
|
524
|
+
"split" => {
|
|
525
|
+
name: "split",
|
|
526
|
+
description: "returns the string split into a list.",
|
|
527
|
+
examples: [
|
|
528
|
+
"\"a,b\".split(\",\")",
|
|
529
|
+
"\"a b\".split",
|
|
530
|
+
"\"a--b\".split(\"--\")"
|
|
531
|
+
]
|
|
532
|
+
},
|
|
533
|
+
"words" => {
|
|
534
|
+
name: "words",
|
|
535
|
+
description: "returns the words in the string as a list.",
|
|
536
|
+
examples: ["\"hello world\".words", "\"one two\".words", "\"\".words"]
|
|
537
|
+
}
|
|
538
|
+
}.freeze
|
|
539
|
+
|
|
540
|
+
def self.function_documentation(scope)
|
|
541
|
+
return INSTANCE_FUNCTIONS if scope == :instance
|
|
542
|
+
|
|
543
|
+
{}
|
|
544
|
+
end
|
|
545
|
+
|
|
6
546
|
def initialize(*args, **_kargs, &_block)
|
|
7
547
|
self.raw =
|
|
8
548
|
if args.first.is_an?(Class)
|
|
@@ -37,7 +577,10 @@ class Code
|
|
|
37
577
|
when "downcase"
|
|
38
578
|
sig(args)
|
|
39
579
|
code_downcase
|
|
40
|
-
when "
|
|
580
|
+
when "lower_case"
|
|
581
|
+
sig(args)
|
|
582
|
+
code_lower_case
|
|
583
|
+
when "include?", "member?"
|
|
41
584
|
sig(args) { String }
|
|
42
585
|
code_include?(code_value)
|
|
43
586
|
when "starts_with?"
|
|
@@ -52,30 +595,165 @@ class Code
|
|
|
52
595
|
when "end_with?"
|
|
53
596
|
sig(args) { String }
|
|
54
597
|
code_end_with?(code_value)
|
|
598
|
+
when "[]", "at", "get"
|
|
599
|
+
sig(args) { Integer }
|
|
600
|
+
code_get(code_value)
|
|
601
|
+
when "capitalize"
|
|
602
|
+
sig(args)
|
|
603
|
+
code_capitalize
|
|
604
|
+
when "characters"
|
|
605
|
+
sig(args)
|
|
606
|
+
code_characters
|
|
607
|
+
when "bytes"
|
|
608
|
+
sig(args)
|
|
609
|
+
code_bytes
|
|
610
|
+
when "bytesize"
|
|
611
|
+
sig(args)
|
|
612
|
+
code_bytesize
|
|
613
|
+
when "byte_slice"
|
|
614
|
+
sig(args) { [Integer, Integer.maybe] }
|
|
615
|
+
code_byte_slice(*code_arguments.raw)
|
|
616
|
+
when "codepoints"
|
|
617
|
+
sig(args)
|
|
618
|
+
code_codepoints
|
|
619
|
+
when "character_code_at"
|
|
620
|
+
sig(args) { Integer.maybe }
|
|
621
|
+
code_character_code_at(code_value)
|
|
622
|
+
when "ordinal"
|
|
623
|
+
sig(args)
|
|
624
|
+
code_ordinal
|
|
625
|
+
when "chomp"
|
|
626
|
+
sig(args)
|
|
627
|
+
code_chomp
|
|
628
|
+
when "chop"
|
|
629
|
+
sig(args)
|
|
630
|
+
code_chop
|
|
631
|
+
when "delete"
|
|
632
|
+
sig(args) { String }
|
|
633
|
+
code_delete(code_value)
|
|
634
|
+
when "delete_prefix"
|
|
635
|
+
sig(args) { String }
|
|
636
|
+
code_delete_prefix(code_value)
|
|
637
|
+
when "delete_suffix"
|
|
638
|
+
sig(args) { String }
|
|
639
|
+
code_delete_suffix(code_value)
|
|
640
|
+
when "empty?"
|
|
641
|
+
sig(args)
|
|
642
|
+
code_empty?
|
|
643
|
+
when "clear"
|
|
644
|
+
sig(args)
|
|
645
|
+
code_clear
|
|
646
|
+
when "count"
|
|
647
|
+
sig(args) { String.repeat(1) }
|
|
648
|
+
code_count(*code_arguments.raw)
|
|
649
|
+
when "insert"
|
|
650
|
+
sig(args) { [Integer, Object] }
|
|
651
|
+
code_insert(*code_arguments.raw)
|
|
652
|
+
when "prepend"
|
|
653
|
+
sig(args) { Object }
|
|
654
|
+
code_prepend(code_value)
|
|
655
|
+
when "concat"
|
|
656
|
+
sig(args) { Object.repeat(1) }
|
|
657
|
+
code_concat(*code_arguments.raw)
|
|
55
658
|
when "first"
|
|
56
659
|
sig(args) { Integer.maybe }
|
|
57
660
|
code_first(code_value)
|
|
661
|
+
when "index"
|
|
662
|
+
sig(args) { String }
|
|
663
|
+
code_index(code_value)
|
|
664
|
+
when "last"
|
|
665
|
+
sig(args) { Integer.maybe }
|
|
666
|
+
code_last(code_value)
|
|
667
|
+
when "lines"
|
|
668
|
+
sig(args)
|
|
669
|
+
code_lines
|
|
58
670
|
when "reverse"
|
|
59
671
|
sig(args)
|
|
60
672
|
code_reverse
|
|
673
|
+
when "right_index"
|
|
674
|
+
sig(args) { String }
|
|
675
|
+
code_right_index(code_value)
|
|
61
676
|
when "parameterize"
|
|
62
677
|
sig(args)
|
|
63
678
|
code_parameterize
|
|
679
|
+
when "squish"
|
|
680
|
+
sig(args)
|
|
681
|
+
code_squish
|
|
682
|
+
when "squeeze"
|
|
683
|
+
sig(args) { String.repeat }
|
|
684
|
+
code_squeeze(*code_arguments.raw)
|
|
64
685
|
when "substitute"
|
|
65
686
|
sig(args) { [String, String.maybe] }
|
|
66
687
|
code_substitute(*code_arguments.raw)
|
|
688
|
+
when "substitute!"
|
|
689
|
+
sig(args) { [String, String.maybe] }
|
|
690
|
+
code_substitute!(*code_arguments.raw)
|
|
691
|
+
when "substitute_all"
|
|
692
|
+
sig(args) { [String, String.maybe] }
|
|
693
|
+
code_substitute_all(*code_arguments.raw)
|
|
694
|
+
when "substitute_all!"
|
|
695
|
+
sig(args) { [String, String.maybe] }
|
|
696
|
+
code_substitute_all!(*code_arguments.raw)
|
|
697
|
+
when "substitute_once"
|
|
698
|
+
sig(args) { [String, String.maybe] }
|
|
699
|
+
code_substitute_once(*code_arguments.raw)
|
|
700
|
+
when "substitute_once!"
|
|
701
|
+
sig(args) { [String, String.maybe] }
|
|
702
|
+
code_substitute_once!(*code_arguments.raw)
|
|
703
|
+
when "swapcase"
|
|
704
|
+
sig(args)
|
|
705
|
+
code_swapcase
|
|
706
|
+
when "titleize"
|
|
707
|
+
sig(args)
|
|
708
|
+
code_titleize
|
|
67
709
|
when "upcase"
|
|
68
710
|
sig(args)
|
|
69
711
|
code_upcase
|
|
70
|
-
when "
|
|
712
|
+
when "upper_case"
|
|
713
|
+
sig(args)
|
|
714
|
+
code_upper_case
|
|
715
|
+
when "size", "length"
|
|
71
716
|
sig(args)
|
|
72
717
|
code_size
|
|
73
718
|
when "strip"
|
|
74
719
|
sig(args)
|
|
75
720
|
code_strip
|
|
721
|
+
when "left_strip"
|
|
722
|
+
sig(args)
|
|
723
|
+
code_left_strip
|
|
724
|
+
when "right_strip"
|
|
725
|
+
sig(args)
|
|
726
|
+
code_right_strip
|
|
727
|
+
when "slice"
|
|
728
|
+
sig(args) { Object.repeat(1) }
|
|
729
|
+
code_slice(*code_arguments.raw)
|
|
730
|
+
when "left_justify"
|
|
731
|
+
sig(args) { [Integer, String.maybe] }
|
|
732
|
+
code_left_justify(*code_arguments.raw)
|
|
733
|
+
when "right_justify"
|
|
734
|
+
sig(args) { [Integer, String.maybe] }
|
|
735
|
+
code_right_justify(*code_arguments.raw)
|
|
736
|
+
when "center"
|
|
737
|
+
sig(args) { [Integer, String.maybe] }
|
|
738
|
+
code_center(*code_arguments.raw)
|
|
739
|
+
when "pad_start"
|
|
740
|
+
sig(args) { [Integer, String.maybe] }
|
|
741
|
+
code_pad_start(*code_arguments.raw)
|
|
742
|
+
when "pad_end"
|
|
743
|
+
sig(args) { [Integer, String.maybe] }
|
|
744
|
+
code_pad_end(*code_arguments.raw)
|
|
745
|
+
when "repeat"
|
|
746
|
+
sig(args) { Integer | Decimal }
|
|
747
|
+
code_repeat(code_value)
|
|
748
|
+
when "substring"
|
|
749
|
+
sig(args) { [Integer.maybe, Integer.maybe] }
|
|
750
|
+
code_substring(*code_arguments.raw)
|
|
76
751
|
when "split"
|
|
77
752
|
sig(args) { String.maybe }
|
|
78
753
|
code_split(code_value)
|
|
754
|
+
when "words"
|
|
755
|
+
sig(args)
|
|
756
|
+
code_words
|
|
79
757
|
else
|
|
80
758
|
super
|
|
81
759
|
end
|
|
@@ -85,20 +763,154 @@ class Code
|
|
|
85
763
|
String.new(raw.downcase)
|
|
86
764
|
end
|
|
87
765
|
|
|
766
|
+
def code_lower_case
|
|
767
|
+
code_downcase
|
|
768
|
+
end
|
|
769
|
+
|
|
88
770
|
def code_upcase
|
|
89
771
|
String.new(raw.upcase)
|
|
90
772
|
end
|
|
91
773
|
|
|
774
|
+
def code_upper_case
|
|
775
|
+
code_upcase
|
|
776
|
+
end
|
|
777
|
+
|
|
778
|
+
def code_capitalize
|
|
779
|
+
String.new(raw.capitalize)
|
|
780
|
+
end
|
|
781
|
+
|
|
782
|
+
def code_characters
|
|
783
|
+
List.new(raw.chars)
|
|
784
|
+
end
|
|
785
|
+
|
|
786
|
+
def code_bytes
|
|
787
|
+
List.new(raw.bytes)
|
|
788
|
+
end
|
|
789
|
+
|
|
790
|
+
def code_bytesize
|
|
791
|
+
Integer.new(raw.bytesize)
|
|
792
|
+
end
|
|
793
|
+
|
|
794
|
+
def code_byte_slice(index, length = nil)
|
|
795
|
+
code_index = index.to_code
|
|
796
|
+
code_length = length.to_code
|
|
797
|
+
value =
|
|
798
|
+
if code_length.nothing?
|
|
799
|
+
raw.byteslice(code_index.raw)
|
|
800
|
+
else
|
|
801
|
+
raw.byteslice(code_index.raw, code_length.raw)
|
|
802
|
+
end
|
|
803
|
+
|
|
804
|
+
value.to_code
|
|
805
|
+
end
|
|
806
|
+
|
|
807
|
+
def code_codepoints
|
|
808
|
+
List.new(raw.codepoints)
|
|
809
|
+
end
|
|
810
|
+
|
|
811
|
+
def code_character_code_at(index = nil)
|
|
812
|
+
code_index = index.to_code
|
|
813
|
+
code_index = Integer.new(0) if code_index.nothing?
|
|
814
|
+
|
|
815
|
+
raw.codepoints[code_index.raw].to_code
|
|
816
|
+
end
|
|
817
|
+
|
|
818
|
+
def code_ordinal
|
|
819
|
+
raw.empty? ? Nothing.new : Integer.new(raw.ord)
|
|
820
|
+
end
|
|
821
|
+
|
|
822
|
+
def code_chomp
|
|
823
|
+
String.new(raw.chomp)
|
|
824
|
+
end
|
|
825
|
+
|
|
826
|
+
def code_chop
|
|
827
|
+
String.new(raw.chop)
|
|
828
|
+
end
|
|
829
|
+
|
|
830
|
+
def code_delete(value)
|
|
831
|
+
code_value = value.to_code
|
|
832
|
+
String.new(raw.delete(code_value.raw))
|
|
833
|
+
end
|
|
834
|
+
|
|
835
|
+
def code_delete_prefix(value)
|
|
836
|
+
code_value = value.to_code
|
|
837
|
+
String.new(raw.delete_prefix(code_value.raw))
|
|
838
|
+
end
|
|
839
|
+
|
|
840
|
+
def code_delete_suffix(value)
|
|
841
|
+
code_value = value.to_code
|
|
842
|
+
String.new(raw.delete_suffix(code_value.raw))
|
|
843
|
+
end
|
|
844
|
+
|
|
845
|
+
def code_empty?
|
|
846
|
+
Boolean.new(raw.empty?)
|
|
847
|
+
end
|
|
848
|
+
|
|
849
|
+
def code_clear
|
|
850
|
+
raw.clear
|
|
851
|
+
self
|
|
852
|
+
end
|
|
853
|
+
|
|
854
|
+
def code_count(*selectors)
|
|
855
|
+
code_selectors = selectors.to_code
|
|
856
|
+
|
|
857
|
+
Integer.new(raw.count(*code_selectors.raw.map(&:to_s)))
|
|
858
|
+
end
|
|
859
|
+
|
|
860
|
+
def code_insert(index, value)
|
|
861
|
+
code_index = index.to_code
|
|
862
|
+
code_value = value.to_code
|
|
863
|
+
|
|
864
|
+
raw.insert(code_index.raw, code_value.to_s)
|
|
865
|
+
self
|
|
866
|
+
end
|
|
867
|
+
|
|
868
|
+
def code_prepend(value)
|
|
869
|
+
code_value = value.to_code
|
|
870
|
+
|
|
871
|
+
raw.prepend(code_value.to_s)
|
|
872
|
+
self
|
|
873
|
+
end
|
|
874
|
+
|
|
875
|
+
def code_concat(*values)
|
|
876
|
+
values.each { |value| raw.concat(value.to_code.to_s) }
|
|
877
|
+
self
|
|
878
|
+
end
|
|
879
|
+
|
|
880
|
+
def code_get(value)
|
|
881
|
+
code_value = value.to_code
|
|
882
|
+
raw[code_value.raw].to_code
|
|
883
|
+
end
|
|
884
|
+
|
|
92
885
|
def code_include?(value)
|
|
93
886
|
code_value = value.to_code
|
|
94
887
|
Boolean.new(raw.include?(code_value.raw))
|
|
95
888
|
end
|
|
96
889
|
|
|
890
|
+
def code_index(value)
|
|
891
|
+
code_value = value.to_code
|
|
892
|
+
raw.index(code_value.raw).to_code
|
|
893
|
+
end
|
|
894
|
+
|
|
895
|
+
def code_last(n = nil)
|
|
896
|
+
code_n = n.to_code
|
|
897
|
+
code_n = Integer.new(1) if code_n.nothing?
|
|
898
|
+
String.new(raw.last(code_n.raw))
|
|
899
|
+
end
|
|
900
|
+
|
|
901
|
+
def code_lines
|
|
902
|
+
List.new(raw.lines)
|
|
903
|
+
end
|
|
904
|
+
|
|
97
905
|
def code_multiplication(other)
|
|
98
906
|
code_other = other.to_code
|
|
99
907
|
String.new(raw * code_other.raw)
|
|
100
908
|
end
|
|
101
909
|
|
|
910
|
+
def code_repeat(other)
|
|
911
|
+
code_multiplication(other)
|
|
912
|
+
end
|
|
913
|
+
|
|
102
914
|
def code_starts_with?(value)
|
|
103
915
|
code_value = value.to_code
|
|
104
916
|
Boolean.new(raw.start_with?(code_value.raw))
|
|
@@ -126,8 +938,35 @@ class Code
|
|
|
126
938
|
String.new(raw.reverse)
|
|
127
939
|
end
|
|
128
940
|
|
|
941
|
+
def code_right_index(value)
|
|
942
|
+
code_value = value.to_code
|
|
943
|
+
raw.rindex(code_value.raw).to_code
|
|
944
|
+
end
|
|
945
|
+
|
|
129
946
|
def code_to_function(**_globals)
|
|
130
|
-
|
|
947
|
+
unless /\A[A-Za-z_][A-Za-z0-9_]*[!?]?\z/.match?(raw)
|
|
948
|
+
raise Error, "String#to_function: invalid function name"
|
|
949
|
+
end
|
|
950
|
+
|
|
951
|
+
Function.new(
|
|
952
|
+
[{ name: "_" }],
|
|
953
|
+
Node::Code.new(
|
|
954
|
+
[
|
|
955
|
+
{
|
|
956
|
+
left_operation: {
|
|
957
|
+
first: {
|
|
958
|
+
call: {
|
|
959
|
+
name: "_"
|
|
960
|
+
}
|
|
961
|
+
},
|
|
962
|
+
others: [
|
|
963
|
+
{ operator: ".", statement: { call: { name: raw } } }
|
|
964
|
+
]
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
]
|
|
968
|
+
)
|
|
969
|
+
)
|
|
131
970
|
end
|
|
132
971
|
|
|
133
972
|
def code_inspect
|
|
@@ -138,13 +977,56 @@ class Code
|
|
|
138
977
|
String.new(raw.parameterize)
|
|
139
978
|
end
|
|
140
979
|
|
|
980
|
+
def code_squish
|
|
981
|
+
String.new(raw.squish)
|
|
982
|
+
end
|
|
983
|
+
|
|
984
|
+
def code_squeeze(*selectors)
|
|
985
|
+
code_selectors = selectors.to_code
|
|
986
|
+
|
|
987
|
+
String.new(raw.squeeze(*code_selectors.raw.map(&:to_s)))
|
|
988
|
+
end
|
|
989
|
+
|
|
141
990
|
def code_substitute(from = nil, to = nil)
|
|
991
|
+
code_substitute_all(from, to)
|
|
992
|
+
end
|
|
993
|
+
|
|
994
|
+
def code_substitute!(from = nil, to = nil)
|
|
995
|
+
code_substitute_all!(from, to)
|
|
996
|
+
end
|
|
997
|
+
|
|
998
|
+
def code_substitute_all(from = nil, to = nil)
|
|
142
999
|
code_from = from.to_code
|
|
143
1000
|
code_to = to.to_code
|
|
144
1001
|
|
|
145
1002
|
String.new(raw.gsub(code_from.to_s, code_to.to_s))
|
|
146
1003
|
end
|
|
147
1004
|
|
|
1005
|
+
def code_substitute_all!(from = nil, to = nil)
|
|
1006
|
+
self.raw = code_substitute_all(from, to).raw
|
|
1007
|
+
self
|
|
1008
|
+
end
|
|
1009
|
+
|
|
1010
|
+
def code_substitute_once(from = nil, to = nil)
|
|
1011
|
+
code_from = from.to_code
|
|
1012
|
+
code_to = to.to_code
|
|
1013
|
+
|
|
1014
|
+
String.new(raw.sub(code_from.to_s, code_to.to_s))
|
|
1015
|
+
end
|
|
1016
|
+
|
|
1017
|
+
def code_substitute_once!(from = nil, to = nil)
|
|
1018
|
+
self.raw = code_substitute_once(from, to).raw
|
|
1019
|
+
self
|
|
1020
|
+
end
|
|
1021
|
+
|
|
1022
|
+
def code_swapcase
|
|
1023
|
+
String.new(raw.swapcase)
|
|
1024
|
+
end
|
|
1025
|
+
|
|
1026
|
+
def code_titleize
|
|
1027
|
+
String.new(raw.titleize)
|
|
1028
|
+
end
|
|
1029
|
+
|
|
148
1030
|
def code_first(n = nil)
|
|
149
1031
|
code_n = n.to_code
|
|
150
1032
|
code_n = Integer.new(1) if code_n.nothing?
|
|
@@ -163,6 +1045,85 @@ class Code
|
|
|
163
1045
|
String.new(sanitized_utf8_raw.strip)
|
|
164
1046
|
end
|
|
165
1047
|
|
|
1048
|
+
def code_left_strip
|
|
1049
|
+
String.new(raw.lstrip)
|
|
1050
|
+
end
|
|
1051
|
+
|
|
1052
|
+
def code_right_strip
|
|
1053
|
+
String.new(raw.rstrip)
|
|
1054
|
+
end
|
|
1055
|
+
|
|
1056
|
+
def code_slice(*arguments)
|
|
1057
|
+
code_arguments = arguments.to_code.raw
|
|
1058
|
+
|
|
1059
|
+
if code_arguments.first.is_a?(Range)
|
|
1060
|
+
code_range = code_arguments.first
|
|
1061
|
+
range =
|
|
1062
|
+
::Range.new(
|
|
1063
|
+
code_range.code_left.to_i,
|
|
1064
|
+
code_range.code_right.to_i,
|
|
1065
|
+
code_range.exclude_end?
|
|
1066
|
+
)
|
|
1067
|
+
|
|
1068
|
+
return raw.slice(range).to_code
|
|
1069
|
+
end
|
|
1070
|
+
|
|
1071
|
+
raw.slice(*code_arguments.map(&:to_i)).to_code
|
|
1072
|
+
end
|
|
1073
|
+
|
|
1074
|
+
def code_left_justify(width, padding = nil)
|
|
1075
|
+
code_width = width.to_code
|
|
1076
|
+
code_padding = padding.to_code
|
|
1077
|
+
code_padding = String.new(" ") if code_padding.nothing?
|
|
1078
|
+
|
|
1079
|
+
String.new(raw.ljust(code_width.raw, code_padding.to_s))
|
|
1080
|
+
end
|
|
1081
|
+
|
|
1082
|
+
def code_right_justify(width, padding = nil)
|
|
1083
|
+
code_width = width.to_code
|
|
1084
|
+
code_padding = padding.to_code
|
|
1085
|
+
code_padding = String.new(" ") if code_padding.nothing?
|
|
1086
|
+
|
|
1087
|
+
String.new(raw.rjust(code_width.raw, code_padding.to_s))
|
|
1088
|
+
end
|
|
1089
|
+
|
|
1090
|
+
def code_center(width, padding = nil)
|
|
1091
|
+
code_width = width.to_code
|
|
1092
|
+
code_padding = padding.to_code
|
|
1093
|
+
code_padding = String.new(" ") if code_padding.nothing?
|
|
1094
|
+
|
|
1095
|
+
String.new(raw.center(code_width.raw, code_padding.to_s))
|
|
1096
|
+
end
|
|
1097
|
+
|
|
1098
|
+
def code_pad_start(width, padding = nil)
|
|
1099
|
+
code_width = width.to_code
|
|
1100
|
+
code_padding = padding.to_code
|
|
1101
|
+
code_padding = String.new(" ") if code_padding.nothing?
|
|
1102
|
+
|
|
1103
|
+
String.new(raw.rjust(code_width.raw, code_padding.to_s))
|
|
1104
|
+
end
|
|
1105
|
+
|
|
1106
|
+
def code_pad_end(width, padding = nil)
|
|
1107
|
+
code_width = width.to_code
|
|
1108
|
+
code_padding = padding.to_code
|
|
1109
|
+
code_padding = String.new(" ") if code_padding.nothing?
|
|
1110
|
+
|
|
1111
|
+
String.new(raw.ljust(code_width.raw, code_padding.to_s))
|
|
1112
|
+
end
|
|
1113
|
+
|
|
1114
|
+
def code_substring(start = nil, finish = nil)
|
|
1115
|
+
code_start = start.to_code
|
|
1116
|
+
code_finish = finish.to_code
|
|
1117
|
+
start_index = code_start.nothing? ? 0 : code_start.raw
|
|
1118
|
+
finish_index = code_finish.nothing? ? raw.length : code_finish.raw
|
|
1119
|
+
start_index = 0 if start_index.negative?
|
|
1120
|
+
finish_index = 0 if finish_index.negative?
|
|
1121
|
+
start_index, finish_index = finish_index, start_index if start_index >
|
|
1122
|
+
finish_index
|
|
1123
|
+
|
|
1124
|
+
String.new(raw[start_index...finish_index].to_s)
|
|
1125
|
+
end
|
|
1126
|
+
|
|
166
1127
|
def code_split(value)
|
|
167
1128
|
code_value = value.to_code
|
|
168
1129
|
|
|
@@ -173,6 +1134,10 @@ class Code
|
|
|
173
1134
|
end
|
|
174
1135
|
end
|
|
175
1136
|
|
|
1137
|
+
def code_words
|
|
1138
|
+
List.new(raw.split)
|
|
1139
|
+
end
|
|
1140
|
+
|
|
176
1141
|
def present?
|
|
177
1142
|
raw.present?
|
|
178
1143
|
end
|