refinements 8.5.0 → 8.5.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
- checksums.yaml.gz.sig +0 -0
- data/README.adoc +248 -233
- data/lib/refinements/identity.rb +1 -1
- data/lib/refinements/pathnames.rb +6 -2
- data/lib/refinements/strings.rb +11 -27
- data.tar.gz.sig +0 -0
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4dc374c744c7048ce100d331ab8e7b371582cf7f51a172ab2d946439b09884e
|
4
|
+
data.tar.gz: a0da5c502e385cbe3271e9a298960f856029e98ba9541e9d8c0cb91c33637241
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6855249021c392a26a9d3b85e12fd357ac4140bed632fd4c2a316d124fb8c47bcb1e24c3f76da2af6b3b40f6d06d01f96f42ad4987bf13a13175187cfe63a915
|
7
|
+
data.tar.gz: 5e064f823da67d4085cbcec41d00a3423b7abba5987514bbfd787f359ac62eb3b8820113fe1488b73f4cc77a84c807fc8fb87137be77ccf0cddd8220f70e035b
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.adoc
CHANGED
@@ -11,9 +11,12 @@ image::https://img.shields.io/badge/code_style-alchemists-brightgreen.svg[Alchem
|
|
11
11
|
[link=https://circleci.com/gh/bkuhlmann/refinements]
|
12
12
|
image::https://circleci.com/gh/bkuhlmann/refinements.svg?style=svg[Circle CI Status]
|
13
13
|
|
14
|
-
Refinements
|
15
|
-
painful and hard to debug
|
16
|
-
|
14
|
+
Refinements are a collection of enhancements to primitive Ruby objects without needing to resort to
|
15
|
+
painful and hard to debug
|
16
|
+
link:https://www.alchemists.io/articles/ruby_antipatterns/#_monkey_patching[monkey patches]. These
|
17
|
+
refinements give you additional syntactic sugar to develop clean and concise implementations while
|
18
|
+
using less code. By refining our code we can acquire the functionality we wish the core primitives
|
19
|
+
had!
|
17
20
|
|
18
21
|
toc::[]
|
19
22
|
|
@@ -70,6 +73,7 @@ gem "refinements", require: false
|
|
70
73
|
----
|
71
74
|
require "refinements/arrays"
|
72
75
|
require "refinements/big_decimals"
|
76
|
+
require "refinements/classes"
|
73
77
|
require "refinements/date_times"
|
74
78
|
require "refinements/hashes"
|
75
79
|
require "refinements/ios"
|
@@ -77,6 +81,7 @@ require "refinements/pathnames"
|
|
77
81
|
require "refinements/strings"
|
78
82
|
require "refinements/string_ios"
|
79
83
|
require "refinements/structs"
|
84
|
+
require "refinements/symbols"
|
80
85
|
----
|
81
86
|
|
82
87
|
=== Using
|
@@ -89,6 +94,7 @@ refinement(s):
|
|
89
94
|
class Example
|
90
95
|
using Refinements::Arrays
|
91
96
|
using Refinements::BigDecimals
|
97
|
+
using Refinements::Classes
|
92
98
|
using Refinements::DateTimes
|
93
99
|
using Refinements::Hashes
|
94
100
|
using Refinements::IOs
|
@@ -96,6 +102,7 @@ class Example
|
|
96
102
|
using Refinements::Strings
|
97
103
|
using Refinements::StringIOs
|
98
104
|
using Refinements::Structs
|
105
|
+
using Refinements::Symbols
|
99
106
|
end
|
100
107
|
----
|
101
108
|
|
@@ -113,8 +120,9 @@ Removes `nil` and empty objects without mutating itself.
|
|
113
120
|
----
|
114
121
|
object = Object.new
|
115
122
|
example = [1, "blueberry", nil, "", [], {}, object]
|
116
|
-
|
117
|
-
example
|
123
|
+
|
124
|
+
example.compress # [1, "blueberry", object]
|
125
|
+
example # [1, "blueberry", nil, "", [], {}, object]
|
118
126
|
----
|
119
127
|
|
120
128
|
===== #compress!
|
@@ -125,8 +133,9 @@ Removes `nil` and empty values while mutating itself.
|
|
125
133
|
----
|
126
134
|
object = Object.new
|
127
135
|
example = [1, "blueberry", nil, "", [], {}, object]
|
128
|
-
|
129
|
-
example
|
136
|
+
|
137
|
+
example.compress # [1, "blueberry", object]
|
138
|
+
example # [1, "blueberry", object]
|
130
139
|
----
|
131
140
|
|
132
141
|
===== #excluding
|
@@ -135,8 +144,8 @@ Removes given array or elements without mutating itself.
|
|
135
144
|
|
136
145
|
[source,ruby]
|
137
146
|
----
|
138
|
-
[1, 2, 3, 4, 5].excluding [4, 5] #
|
139
|
-
[1, 2, 3, 4, 5].excluding 4, 5 #
|
147
|
+
[1, 2, 3, 4, 5].excluding [4, 5] # [1, 2, 3]
|
148
|
+
[1, 2, 3, 4, 5].excluding 4, 5 # [1, 2, 3]
|
140
149
|
----
|
141
150
|
|
142
151
|
===== #filter_find
|
@@ -151,9 +160,9 @@ handlers = [
|
|
151
160
|
->(object) { object if object == :a }
|
152
161
|
]
|
153
162
|
|
154
|
-
handlers.filter_find #
|
155
|
-
handlers.filter_find { |handler| handler.call :a } #
|
156
|
-
handlers.filter_find { |handler| handler.call :x } #
|
163
|
+
handlers.filter_find # Enumerator::Lazy
|
164
|
+
handlers.filter_find { |handler| handler.call :a } # :a
|
165
|
+
handlers.filter_find { |handler| handler.call :x } # nil
|
157
166
|
----
|
158
167
|
|
159
168
|
===== #including
|
@@ -162,8 +171,8 @@ Adds given array or elements without mutating itself.
|
|
162
171
|
|
163
172
|
[source,ruby]
|
164
173
|
----
|
165
|
-
[1, 2, 3].including [4, 5] #
|
166
|
-
[1, 2, 3].including 4, 5 #
|
174
|
+
[1, 2, 3].including [4, 5] # [1, 2, 3, 4, 5]
|
175
|
+
[1, 2, 3].including 4, 5 # [1, 2, 3, 4, 5]
|
167
176
|
----
|
168
177
|
|
169
178
|
===== #intersperse
|
@@ -172,9 +181,9 @@ Inserts additional elements or array between all members of given array.
|
|
172
181
|
|
173
182
|
[source,ruby]
|
174
183
|
----
|
175
|
-
[1, 2, 3].intersperse :a #
|
176
|
-
[1, 2, 3].intersperse :a, :b #
|
177
|
-
[1, 2, 3].intersperse %i[a b c] #
|
184
|
+
[1, 2, 3].intersperse :a # [1, :a, 2, :a, 3]
|
185
|
+
[1, 2, 3].intersperse :a, :b # [1, :a, :b, 2, :a, :b, 3]
|
186
|
+
[1, 2, 3].intersperse %i[a b c] # [1, :a, :b, :c, 2, :a, :b, :c, 3]
|
178
187
|
----
|
179
188
|
|
180
189
|
===== #maximum
|
@@ -186,8 +195,8 @@ Answers the maximum extracted value from a collection of objects.
|
|
186
195
|
Point = Struct.new :x, :y, keyword_init: true
|
187
196
|
points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
|
188
197
|
|
189
|
-
points.maximum(:x) #
|
190
|
-
points.maximum(:y) #
|
198
|
+
points.maximum(:x) # 2
|
199
|
+
points.maximum(:y) # 3
|
191
200
|
----
|
192
201
|
|
193
202
|
===== #mean
|
@@ -196,10 +205,10 @@ Answers mean/average all elements within an array.
|
|
196
205
|
|
197
206
|
[source,ruby]
|
198
207
|
----
|
199
|
-
[].mean #
|
200
|
-
[5].mean #
|
201
|
-
[1, 2, 3].mean #
|
202
|
-
[1.25, 1.5, 1.75].mean #
|
208
|
+
[].mean # 0
|
209
|
+
[5].mean # 5
|
210
|
+
[1, 2, 3].mean # 2
|
211
|
+
[1.25, 1.5, 1.75].mean # 1.5
|
203
212
|
----
|
204
213
|
|
205
214
|
===== #minimum
|
@@ -211,8 +220,8 @@ Answers the minimum extracted value from a collection of objects.
|
|
211
220
|
Point = Struct.new :x, :y, keyword_init: true
|
212
221
|
points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
|
213
222
|
|
214
|
-
points.minimum(:x) #
|
215
|
-
points.minimum(:y) #
|
223
|
+
points.minimum(:x) # 0
|
224
|
+
points.minimum(:y) # 1
|
216
225
|
----
|
217
226
|
|
218
227
|
===== #pad
|
@@ -222,9 +231,9 @@ needs to be a specific size with padded values.
|
|
222
231
|
|
223
232
|
[source,ruby]
|
224
233
|
----
|
225
|
-
[1].pad 0 #
|
226
|
-
[1].pad 0, max: 3 #
|
227
|
-
[1, 2].pad 3, max: 3 #
|
234
|
+
[1].pad 0 # [1]
|
235
|
+
[1].pad 0, max: 3 # [1, 0, 0]
|
236
|
+
[1, 2].pad 3, max: 3 # [1, 2, 3]
|
228
237
|
----
|
229
238
|
|
230
239
|
===== #ring
|
@@ -234,7 +243,7 @@ Answers a circular array which can enumerate before, current, after elements.
|
|
234
243
|
[source,ruby]
|
235
244
|
----
|
236
245
|
example = [1, 2, 3]
|
237
|
-
example.ring
|
246
|
+
example.ring # "#<Enumerator: ...>"
|
238
247
|
example.ring { |(before, current, after)| puts "#{before} #{current} #{after}" }
|
239
248
|
|
240
249
|
# [3 1 2]
|
@@ -250,7 +259,7 @@ Allows one to inspect a big decimal with numeric representation.
|
|
250
259
|
|
251
260
|
[source,ruby]
|
252
261
|
----
|
253
|
-
BigDecimal.new("5.0E-10").inspect
|
262
|
+
BigDecimal.new("5.0E-10").inspect # "#<BigDecimal:3fd3d458fe84 0.0000000005>"
|
254
263
|
----
|
255
264
|
|
256
265
|
==== Class
|
@@ -265,8 +274,8 @@ a = Class.new
|
|
265
274
|
b = Class.new a
|
266
275
|
c = Class.new a
|
267
276
|
|
268
|
-
a.descendants #
|
269
|
-
Class.new.descendants #
|
277
|
+
a.descendants # [b, c]
|
278
|
+
Class.new.descendants # []
|
270
279
|
----
|
271
280
|
|
272
281
|
==== DateTime
|
@@ -277,7 +286,7 @@ Answers new DateTime object for current UTC date/time.
|
|
277
286
|
|
278
287
|
[source,ruby]
|
279
288
|
----
|
280
|
-
DateTime.utc #
|
289
|
+
DateTime.utc # "#<DateTime: 2019-12-31T18:17:00+00:00 ((2458849j,65820s,181867000n),+0s,2299161j)>"
|
281
290
|
----
|
282
291
|
|
283
292
|
==== Hash
|
@@ -289,8 +298,8 @@ Answers new hash where missing keys, even deeply nested, answer an empty hash.
|
|
289
298
|
[source,ruby]
|
290
299
|
----
|
291
300
|
example = Hash.infinite
|
292
|
-
example[:a] #
|
293
|
-
example[:a][:b][:c] #
|
301
|
+
example[:a] # {}
|
302
|
+
example[:a][:b][:c] # {}
|
294
303
|
----
|
295
304
|
|
296
305
|
===== .with_default
|
@@ -300,10 +309,10 @@ Answers new hash where every top-level missing key has the same default value.
|
|
300
309
|
[source,ruby]
|
301
310
|
----
|
302
311
|
example = Hash.with_default ""
|
303
|
-
example[:a] #
|
312
|
+
example[:a] # ""
|
304
313
|
|
305
314
|
example = Hash.with_default []
|
306
|
-
example[:b] #
|
315
|
+
example[:b] # []
|
307
316
|
----
|
308
317
|
|
309
318
|
===== #compress
|
@@ -314,8 +323,9 @@ Removes `nil` and empty objects without mutating itself.
|
|
314
323
|
----
|
315
324
|
object = Object.new
|
316
325
|
example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
|
317
|
-
|
318
|
-
example
|
326
|
+
|
327
|
+
example.compress # {a: 1, b: "blueberry", g: object}
|
328
|
+
example # {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
|
319
329
|
----
|
320
330
|
|
321
331
|
===== #compress!
|
@@ -326,8 +336,9 @@ Removes `nil` and empty objects while mutating itself.
|
|
326
336
|
----
|
327
337
|
object = Object.new
|
328
338
|
example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
|
329
|
-
|
330
|
-
example
|
339
|
+
|
340
|
+
example.compress! # {a: 1, b: "blueberry", g: object}
|
341
|
+
example # {a: 1, b: "blueberry", g: object}
|
331
342
|
----
|
332
343
|
|
333
344
|
===== #deep_merge
|
@@ -337,8 +348,9 @@ Merges deeply nested hashes together without mutating itself.
|
|
337
348
|
[source,ruby]
|
338
349
|
----
|
339
350
|
example = {a: "A", b: {one: "One", two: "Two"}}
|
340
|
-
|
341
|
-
example
|
351
|
+
|
352
|
+
example.deep_merge b: {one: 1} # {a: "A", b: {one: 1, two: "Two"}}
|
353
|
+
example # {a: "A", b: {one: "One", two: "Two"}}
|
342
354
|
----
|
343
355
|
|
344
356
|
===== #deep_merge!
|
@@ -348,8 +360,9 @@ Merges deeply nested hashes together while mutating itself.
|
|
348
360
|
[source,ruby]
|
349
361
|
----
|
350
362
|
example = {a: "A", b: {one: "One", two: "Two"}}
|
351
|
-
|
352
|
-
example
|
363
|
+
|
364
|
+
example.deep_merge! b: {one: 1} # {a: "A", b: {one: 1, two: "Two"}}
|
365
|
+
example # {a: "A", b: {one: 1, two: "Two"}}
|
353
366
|
----
|
354
367
|
|
355
368
|
===== #deep_stringify_keys
|
@@ -359,8 +372,8 @@ Stringifies keys of nested hash without mutating itself. Does not handle nested
|
|
359
372
|
[source,ruby]
|
360
373
|
----
|
361
374
|
example = {a: {b: 2}}
|
362
|
-
example.deep_stringify_keys #
|
363
|
-
example #
|
375
|
+
example.deep_stringify_keys # {"a" => {"b" => 1}}
|
376
|
+
example # {a: {b: 2}}
|
364
377
|
----
|
365
378
|
|
366
379
|
===== #deep_stringify_keys!
|
@@ -370,8 +383,8 @@ Stringifies keys of nested hash while mutating itself. Does not handle nested ar
|
|
370
383
|
[source,ruby]
|
371
384
|
----
|
372
385
|
example = {a: {b: 2}}
|
373
|
-
example.deep_stringify_keys! #
|
374
|
-
example #
|
386
|
+
example.deep_stringify_keys! # {"a" => {"b" => 1}}
|
387
|
+
example # {"a" => {"b" => 1}}
|
375
388
|
----
|
376
389
|
|
377
390
|
===== #deep_symbolize_keys
|
@@ -381,8 +394,8 @@ Symbolizes keys of nested hash without mutating itself. Does not handle nested a
|
|
381
394
|
[source,ruby]
|
382
395
|
----
|
383
396
|
example = {"a" => {"b" => 2}}
|
384
|
-
example.deep_symbolize_keys #
|
385
|
-
example #
|
397
|
+
example.deep_symbolize_keys # {a: {b: 1}}
|
398
|
+
example # {"a" => {"b" => 2}}
|
386
399
|
----
|
387
400
|
|
388
401
|
===== #deep_symbolize_keys!
|
@@ -392,8 +405,8 @@ Symbolizes keys of nested hash while mutating itself. Does not handle nested arr
|
|
392
405
|
[source,ruby]
|
393
406
|
----
|
394
407
|
example = {"a" => {"b" => 2}}
|
395
|
-
example.deep_symbolize_keys! #
|
396
|
-
example #
|
408
|
+
example.deep_symbolize_keys! # {a: {b: 1}}
|
409
|
+
example # {a: {b: 1}}
|
397
410
|
----
|
398
411
|
|
399
412
|
===== #fetch_value
|
@@ -404,12 +417,12 @@ expression `example.fetch(:desired_key) || "default_value"`.
|
|
404
417
|
|
405
418
|
[source,ruby]
|
406
419
|
----
|
407
|
-
{a: "test"}.fetch_value :a, "default" #
|
408
|
-
{a: "test"}.fetch_value :a #
|
409
|
-
{a: nil}.fetch_value :a, "default"
|
410
|
-
{}.fetch_value(:a) { "default" }
|
411
|
-
{}.fetch_value :a
|
412
|
-
{a: "test"}.fetch_value
|
420
|
+
{a: "test"}.fetch_value :a, "default" # "test"
|
421
|
+
{a: "test"}.fetch_value :a # "test"
|
422
|
+
{a: nil}.fetch_value :a, "default" # "default"
|
423
|
+
{}.fetch_value(:a) { "default" } # "default"
|
424
|
+
{}.fetch_value :a # KeyError
|
425
|
+
{a: "test"}.fetch_value # ArgumentError
|
413
426
|
----
|
414
427
|
|
415
428
|
===== #flatten_keys
|
@@ -419,15 +432,15 @@ though.
|
|
419
432
|
|
420
433
|
[source,ruby]
|
421
434
|
----
|
422
|
-
{a: {b: 1}}.flatten_keys prefix: :test
|
423
|
-
{a: {b: 1}}.flatten_keys delimiter: :|
|
435
|
+
{a: {b: 1}}.flatten_keys prefix: :test # {test_a_b: 1}
|
436
|
+
{a: {b: 1}}.flatten_keys delimiter: :| # {:"a|b" => 1}
|
424
437
|
|
425
|
-
{a: {b: 1}}.flatten_keys cast: :to_s #
|
426
|
-
{"a" => {"b" => 1}}.flatten_keys cast: :to_sym #
|
438
|
+
{a: {b: 1}}.flatten_keys cast: :to_s # {"a_b" => 1}
|
439
|
+
{"a" => {"b" => 1}}.flatten_keys cast: :to_sym # {a_b: 1}
|
427
440
|
|
428
441
|
example = {a: {b: 1}}
|
429
|
-
example.flatten_keys
|
430
|
-
example
|
442
|
+
example.flatten_keys # {a_b: 1}
|
443
|
+
example # {a: {b: 1}}
|
431
444
|
----
|
432
445
|
|
433
446
|
===== #flatten_keys!
|
@@ -438,8 +451,8 @@ though.
|
|
438
451
|
[source,ruby]
|
439
452
|
----
|
440
453
|
example = {a: {b: 1}}
|
441
|
-
example.flatten_keys! #
|
442
|
-
example #
|
454
|
+
example.flatten_keys! # {a_b: 1}
|
455
|
+
example # {a_b: 1}
|
443
456
|
----
|
444
457
|
|
445
458
|
===== #recurse
|
@@ -450,8 +463,8 @@ handle nested arrays, though.
|
|
450
463
|
[source,ruby]
|
451
464
|
----
|
452
465
|
example = {"a" => {"b" => 1}}
|
453
|
-
example.recurse(&:symbolize_keys) #
|
454
|
-
example.recurse(&:invert) #
|
466
|
+
example.recurse(&:symbolize_keys) # {a: {b: 1}}
|
467
|
+
example.recurse(&:invert) # {{"b" => 1} => "a"}
|
455
468
|
----
|
456
469
|
|
457
470
|
===== #stringify_keys
|
@@ -461,8 +474,8 @@ Converts keys to strings without mutating itself.
|
|
461
474
|
[source,ruby]
|
462
475
|
----
|
463
476
|
example = {a: 1, b: 2}
|
464
|
-
example.stringify_keys #
|
465
|
-
example #
|
477
|
+
example.stringify_keys # {"a" => 1, "b" => 2}
|
478
|
+
example # {a: 1, b: 2}
|
466
479
|
----
|
467
480
|
|
468
481
|
===== #stringify_keys!
|
@@ -472,8 +485,8 @@ Converts keys to strings while mutating itself.
|
|
472
485
|
[source,ruby]
|
473
486
|
----
|
474
487
|
example = {a: 1, b: 2}
|
475
|
-
example.stringify_keys! #
|
476
|
-
example #
|
488
|
+
example.stringify_keys! # {"a" => 1, "b" => 2}
|
489
|
+
example # {"a" => 1, "b" => 2}
|
477
490
|
----
|
478
491
|
|
479
492
|
===== #symbolize_keys
|
@@ -483,8 +496,8 @@ Converts keys to symbols without mutating itself.
|
|
483
496
|
[source,ruby]
|
484
497
|
----
|
485
498
|
example = {"a" => 1, "b" => 2}
|
486
|
-
example.symbolize_keys #
|
487
|
-
example #
|
499
|
+
example.symbolize_keys # {a: 1, b: 2}
|
500
|
+
example # {"a" => 1, "b" => 2}
|
488
501
|
----
|
489
502
|
|
490
503
|
===== #symbolize_keys!
|
@@ -494,8 +507,8 @@ Converts keys to symbols while mutating itself.
|
|
494
507
|
[source,ruby]
|
495
508
|
----
|
496
509
|
example = {"a" => 1, "b" => 2}
|
497
|
-
example.symbolize_keys! #
|
498
|
-
example #
|
510
|
+
example.symbolize_keys! # {a: 1, b: 2}
|
511
|
+
example # {a: 1, b: 2}
|
499
512
|
----
|
500
513
|
|
501
514
|
===== #use
|
@@ -505,7 +518,8 @@ Passes each hash value as a block argument for further processing.
|
|
505
518
|
[source,ruby]
|
506
519
|
----
|
507
520
|
example = {unit: "221B", street: "Baker Street", city: "London", country: "UK"}
|
508
|
-
|
521
|
+
|
522
|
+
example.use { |unit, street| "#{unit} #{street}" } # "221B Baker Street"
|
509
523
|
----
|
510
524
|
|
511
525
|
==== IO
|
@@ -518,8 +532,8 @@ block, you'll need to close the stream manually.
|
|
518
532
|
|
519
533
|
[source,ruby]
|
520
534
|
----
|
521
|
-
io = IO.void #
|
522
|
-
io = IO.void { |void| void.write "nevermore" } #
|
535
|
+
io = IO.void # "#<IO:fd 20>"
|
536
|
+
io = IO.void { |void| void.write "nevermore" } # "#<IO:(closed)>"
|
523
537
|
----
|
524
538
|
|
525
539
|
===== #redirect
|
@@ -532,8 +546,8 @@ answered instead.
|
|
532
546
|
io = IO.new IO.sysopen(Pathname("test.txt").to_s, "w+")
|
533
547
|
other = IO.new IO.sysopen(Pathname("other.txt").to_s, "w+")
|
534
548
|
|
535
|
-
io.redirect other #
|
536
|
-
io.redirect(other) { |stream| stream.write "test" } #
|
549
|
+
io.redirect other # "#<IO:fd 20>"
|
550
|
+
io.redirect(other) { |stream| stream.write "test" } # "#<IO:fd 21>"
|
537
551
|
----
|
538
552
|
|
539
553
|
===== #reread
|
@@ -545,12 +559,12 @@ Answers full stream by rewinding to beginning of stream and reading all content.
|
|
545
559
|
io = IO.new IO.sysopen(Pathname("test.txt").to_s, "w+")
|
546
560
|
io.write "This is a test."
|
547
561
|
|
548
|
-
io.reread
|
549
|
-
io.reread 4
|
562
|
+
io.reread # "This is a test."
|
563
|
+
io.reread 4 # "This"
|
550
564
|
|
551
565
|
buffer = "".dup
|
552
|
-
io.reread(buffer: buffer) #
|
553
|
-
buffer #
|
566
|
+
io.reread(buffer: buffer) # "This is a test."
|
567
|
+
buffer # "This is a test."
|
554
568
|
----
|
555
569
|
|
556
570
|
===== #squelch
|
@@ -561,9 +575,10 @@ arguments or when given a block.
|
|
561
575
|
[source,ruby]
|
562
576
|
----
|
563
577
|
io = IO.new IO.sysopen(Pathname("test.txt").to_s, "w+")
|
564
|
-
|
565
|
-
io.squelch
|
566
|
-
io.
|
578
|
+
|
579
|
+
io.squelch # "#<IO:fd 20>"
|
580
|
+
io.squelch { io.write "Test" } # "#<IO:fd 20>"
|
581
|
+
io.reread # ""
|
567
582
|
----
|
568
583
|
|
569
584
|
==== Pathname
|
@@ -577,7 +592,7 @@ construct a valid path.
|
|
577
592
|
|
578
593
|
[source,ruby]
|
579
594
|
----
|
580
|
-
Pathname(nil)
|
595
|
+
Pathname(nil) # Pathname("")
|
581
596
|
----
|
582
597
|
|
583
598
|
===== .home
|
@@ -586,7 +601,7 @@ Answers user home directory.
|
|
586
601
|
|
587
602
|
[source,ruby]
|
588
603
|
----
|
589
|
-
Pathname.home #
|
604
|
+
Pathname.home # Pathname "/Users/demo"
|
590
605
|
----
|
591
606
|
|
592
607
|
===== .make_temp_dir
|
@@ -603,13 +618,13 @@ reality, these paths will be longer depending on which operating system you are
|
|
603
618
|
|
604
619
|
[source,ruby]
|
605
620
|
----
|
606
|
-
Pathname.make_temp_dir #
|
607
|
-
Pathname.make_temp_dir prefix: "prefix-" #
|
608
|
-
Pathname.make_temp_dir suffix: "-suffix" #
|
609
|
-
Pathname.make_temp_dir prefix: "prefix-", suffix: "-suffix" #
|
610
|
-
Pathname.make_temp_dir root: "/example" #
|
611
|
-
Pathname.make_temp_dir { "I am a block result" } #
|
612
|
-
Pathname.make_temp_dir { |path| path.join "sub_dir" } #
|
621
|
+
Pathname.make_temp_dir # Pathname:/var/folders/T/temp-20200101-16940-r8
|
622
|
+
Pathname.make_temp_dir prefix: "prefix-" # Pathname:/var/folders/T/prefix-20200101-16940-r8
|
623
|
+
Pathname.make_temp_dir suffix: "-suffix" # Pathname:/var/folders/T/temp-20200101-16940-r8-suffix
|
624
|
+
Pathname.make_temp_dir prefix: "prefix-", suffix: "-suffix" # Pathname:/var/folders/T/prefix-20200101-16940-r8-suffix
|
625
|
+
Pathname.make_temp_dir root: "/example" # Pathname:/example/temp-20200101-16940-r8
|
626
|
+
Pathname.make_temp_dir { "I am a block result" } # "I am a block result"
|
627
|
+
Pathname.make_temp_dir { |path| path.join "sub_dir" } # Pathname:/var/folders/T/temp-20200101-16940-r8/sub_dir
|
613
628
|
----
|
614
629
|
|
615
630
|
===== .require_tree
|
@@ -646,7 +661,7 @@ Answers operating system root path.
|
|
646
661
|
|
647
662
|
[source,ruby]
|
648
663
|
----
|
649
|
-
Pathname.root #
|
664
|
+
Pathname.root # Pathname "/"
|
650
665
|
----
|
651
666
|
|
652
667
|
===== #change_dir
|
@@ -656,13 +671,13 @@ link:https://rubyapi.org/o/Dir.chdir#method-c-chdir[Dir.chdir] for details.
|
|
656
671
|
|
657
672
|
[source,ruby]
|
658
673
|
----
|
659
|
-
Pathname.pwd
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
Pathname.pwd
|
674
|
+
current = Pathname.pwd # "$HOME/demo" (Present Working Directory)
|
675
|
+
custom = current.join("test").make_dir # Pathname "$HOME/demo/test"
|
676
|
+
custom.change_dir # "$HOME/demo/test" (Present Working Directory)
|
677
|
+
current.change_dir # "$HOME/demo" (Present Working Directory)
|
678
|
+
custom.change_dir { "example" } # "example"
|
679
|
+
custom.change_dir { |path| path } # Pathname "$HOME/demo/test"
|
680
|
+
Pathname.pwd # "$HOME/demo" (Present Working Directory)
|
666
681
|
----
|
667
682
|
|
668
683
|
===== #copy
|
@@ -671,7 +686,7 @@ Copies file from current location to new location while answering itself so it c
|
|
671
686
|
|
672
687
|
[source,ruby]
|
673
688
|
----
|
674
|
-
Pathname("input.txt").copy Pathname("output.txt") #
|
689
|
+
Pathname("input.txt").copy Pathname("output.txt") # Pathname("input.txt")
|
675
690
|
----
|
676
691
|
|
677
692
|
===== #deep_touch
|
@@ -681,8 +696,8 @@ directories no matter how deeply nested the file might be.
|
|
681
696
|
|
682
697
|
[source,ruby]
|
683
698
|
----
|
684
|
-
Pathname("a/b/c/d.txt").touch #
|
685
|
-
Pathname("a/b/c/d.txt").touch Time.now - 1 #
|
699
|
+
Pathname("a/b/c/d.txt").touch # Pathname("a/b/c/d.txt")
|
700
|
+
Pathname("a/b/c/d.txt").touch Time.now - 1 # Pathname("a/b/c/d.txt")
|
686
701
|
----
|
687
702
|
|
688
703
|
===== #delete
|
@@ -692,10 +707,10 @@ Deletes file or directory and answers itself so it can be chained.
|
|
692
707
|
[source,ruby]
|
693
708
|
----
|
694
709
|
# When path exists.
|
695
|
-
Pathname("/example.txt").touch.delete #
|
710
|
+
Pathname("/example.txt").touch.delete # Pathname("/example")
|
696
711
|
|
697
712
|
# When path doesn't exist.
|
698
|
-
Pathname("/example.txt").delete #
|
713
|
+
Pathname("/example.txt").delete # Errno::ENOENT
|
699
714
|
----
|
700
715
|
|
701
716
|
===== #directories
|
@@ -704,9 +719,9 @@ Answers all directories or filtered directories for current path.
|
|
704
719
|
|
705
720
|
[source,ruby]
|
706
721
|
----
|
707
|
-
Pathname("/example").directories #
|
708
|
-
Pathname("/example").directories "a*" #
|
709
|
-
Pathname("/example").directories flag: File::FNM_DOTMATCH #
|
722
|
+
Pathname("/example").directories # [Pathname("a"), Pathname("b")]
|
723
|
+
Pathname("/example").directories "a*" # [Pathname("a")]
|
724
|
+
Pathname("/example").directories flag: File::FNM_DOTMATCH # [Pathname(".."), Pathname(".")]
|
710
725
|
----
|
711
726
|
|
712
727
|
===== #empty
|
@@ -719,8 +734,8 @@ of contents. If a directory or file doesn't exist, it will be created.
|
|
719
734
|
directory = Pathname("test").make_path
|
720
735
|
file = directory.join("test.txt").write("example")
|
721
736
|
|
722
|
-
file.empty.read #
|
723
|
-
directory.empty.children #
|
737
|
+
file.empty.read # ""
|
738
|
+
directory.empty.children # []
|
724
739
|
----
|
725
740
|
|
726
741
|
===== #extensions
|
@@ -729,7 +744,7 @@ Answers file extensions as an array.
|
|
729
744
|
|
730
745
|
[source,ruby]
|
731
746
|
----
|
732
|
-
Pathname("example.txt.erb").extensions #
|
747
|
+
Pathname("example.txt.erb").extensions # [".txt", ".erb"]
|
733
748
|
----
|
734
749
|
|
735
750
|
===== #files
|
@@ -738,9 +753,9 @@ Answers all files or filtered files for current path.
|
|
738
753
|
|
739
754
|
[source,ruby]
|
740
755
|
----
|
741
|
-
Pathname("/example").files #
|
742
|
-
Pathname("/example").files "*.png" #
|
743
|
-
Pathname("/example").files flag: File::FNM_DOTMATCH #
|
756
|
+
Pathname("/example").files # [Pathname("a.txt"), Pathname("a.png")]
|
757
|
+
Pathname("/example").files "*.png" # [Pathname("a.png")]
|
758
|
+
Pathname("/example").files flag: File::FNM_DOTMATCH # [Pathname(".ruby-version")]
|
744
759
|
----
|
745
760
|
|
746
761
|
===== #gsub
|
@@ -750,10 +765,10 @@ Same behavior as `String#gsub` but answers a path with patterns replaced with de
|
|
750
765
|
[source,ruby]
|
751
766
|
----
|
752
767
|
Pathname("/a/path/some/path").gsub("path", "test")
|
753
|
-
#
|
768
|
+
# Pathname("/a/test/some/test")
|
754
769
|
|
755
770
|
Pathname("/%placeholder%/some/%placeholder%").gsub("%placeholder%", "test")
|
756
|
-
#
|
771
|
+
# Pathname("/test/some/test")
|
757
772
|
----
|
758
773
|
|
759
774
|
===== #make_ancestors
|
@@ -762,9 +777,9 @@ Ensures all ancestor directories are created for a path.
|
|
762
777
|
|
763
778
|
[source,ruby]
|
764
779
|
----
|
765
|
-
Pathname("/one/two").make_ancestors #
|
766
|
-
Pathname("/one").exist? #
|
767
|
-
Pathname("/one/two").exist? #
|
780
|
+
Pathname("/one/two").make_ancestors # Pathname("/one/two")
|
781
|
+
Pathname("/one").exist? # true
|
782
|
+
Pathname("/one/two").exist? # false
|
768
783
|
----
|
769
784
|
|
770
785
|
===== #make_dir
|
@@ -774,8 +789,8 @@ not throwing errors when directory does exist in order to ensure the pathname ca
|
|
774
789
|
|
775
790
|
[source,ruby]
|
776
791
|
----
|
777
|
-
Pathname("/one").make_dir #
|
778
|
-
Pathname("/one").make_dir.make_dir #
|
792
|
+
Pathname("/one").make_dir # Pathname("/one")
|
793
|
+
Pathname("/one").make_dir.make_dir # Pathname("/one")
|
779
794
|
----
|
780
795
|
|
781
796
|
===== #make_path
|
@@ -785,8 +800,8 @@ not throwing errors when directory does exist in order to ensure the pathname ca
|
|
785
800
|
|
786
801
|
[source,ruby]
|
787
802
|
----
|
788
|
-
Pathname("/one/two/three").make_path #
|
789
|
-
Pathname("/one/two/three").make_path.make_path #
|
803
|
+
Pathname("/one/two/three").make_path # Pathname("/one/two/three")
|
804
|
+
Pathname("/one/two/three").make_path.make_path # Pathname("/one/two/three")
|
790
805
|
----
|
791
806
|
|
792
807
|
===== #name
|
@@ -795,7 +810,7 @@ Answers file name without extension.
|
|
795
810
|
|
796
811
|
[source,ruby]
|
797
812
|
----
|
798
|
-
Pathname("example.txt").name #
|
813
|
+
Pathname("example.txt").name # Pathname("example")
|
799
814
|
----
|
800
815
|
|
801
816
|
===== #relative_parent
|
@@ -804,7 +819,7 @@ Answers relative path from parent directory. This is a complement to `#relative_
|
|
804
819
|
|
805
820
|
[source,ruby]
|
806
821
|
----
|
807
|
-
Pathname("/one/two/three").relative_parent("/one")
|
822
|
+
Pathname("/one/two/three").relative_parent("/one") # Pathname "two"
|
808
823
|
----
|
809
824
|
|
810
825
|
===== #remove_dir
|
@@ -814,9 +829,9 @@ not throwing errors when directory does exist in order to ensure the pathname ca
|
|
814
829
|
|
815
830
|
[source,ruby]
|
816
831
|
----
|
817
|
-
Pathname("/test").make_dir.remove_dir.exist? #
|
818
|
-
Pathname("/test").remove_dir #
|
819
|
-
Pathname("/test").remove_dir.remove_dir #
|
832
|
+
Pathname("/test").make_dir.remove_dir.exist? # false
|
833
|
+
Pathname("/test").remove_dir # Pathname("/test")
|
834
|
+
Pathname("/test").remove_dir.remove_dir # Pathname("/test")
|
820
835
|
----
|
821
836
|
|
822
837
|
===== #remove_tree
|
@@ -830,14 +845,14 @@ parent_path = Pathname "/one"
|
|
830
845
|
child_path = parent_path.join "two"
|
831
846
|
|
832
847
|
child_path.make_path
|
833
|
-
child_path.remove_tree
|
834
|
-
child_path.exist?
|
835
|
-
paremt_path.exist?
|
848
|
+
child_path.remove_tree # Pathname "/one/two"
|
849
|
+
child_path.exist? # false
|
850
|
+
paremt_path.exist? # true
|
836
851
|
|
837
852
|
child_path.make_path
|
838
|
-
parent_path.remove_tree #
|
839
|
-
child_path.exist? #
|
840
|
-
parent_path.exist? #
|
853
|
+
parent_path.remove_tree # Pathname "/one"
|
854
|
+
child_path.exist? # false
|
855
|
+
parent_path.exist? # false
|
841
856
|
----
|
842
857
|
|
843
858
|
===== #rewrite
|
@@ -847,8 +862,8 @@ immediate writing back to the same file.
|
|
847
862
|
|
848
863
|
[source,ruby]
|
849
864
|
----
|
850
|
-
Pathname("/test.txt").rewrite #
|
851
|
-
Pathname("/test.txt").rewrite { |body| body.sub "[token]", "example" } #
|
865
|
+
Pathname("/test.txt").rewrite # Pathname("/test.txt")
|
866
|
+
Pathname("/test.txt").rewrite { |body| body.sub "[token]", "example" } # Pathname("/test.txt")
|
852
867
|
----
|
853
868
|
|
854
869
|
===== #touch
|
@@ -858,10 +873,10 @@ doesn't exist, it will be created as a file.
|
|
858
873
|
|
859
874
|
[source,ruby]
|
860
875
|
----
|
861
|
-
Pathname("example").touch #
|
862
|
-
Pathname("example").touch Time.now - 1 #
|
863
|
-
Pathname("example.txt").touch #
|
864
|
-
Pathname("example.txt").touch Time.now - 1 #
|
876
|
+
Pathname("example").touch # Pathname("example")
|
877
|
+
Pathname("example").touch Time.now - 1 # Pathname("example")
|
878
|
+
Pathname("example.txt").touch # Pathname("example.txt")
|
879
|
+
Pathname("example.txt").touch Time.now - 1 # Pathname("example.txt")
|
865
880
|
----
|
866
881
|
|
867
882
|
===== #write
|
@@ -871,9 +886,9 @@ options.
|
|
871
886
|
|
872
887
|
[source,ruby]
|
873
888
|
----
|
874
|
-
Pathname("example.txt").write "test" #
|
875
|
-
Pathname("example.txt").write "test", offset: 1 #
|
876
|
-
Pathname("example.txt").write "test", mode: "a" #
|
889
|
+
Pathname("example.txt").write "test" # Pathname("example.txt")
|
890
|
+
Pathname("example.txt").write "test", offset: 1 # Pathname("example.txt")
|
891
|
+
Pathname("example.txt").write "test", mode: "a" # Pathname("example.txt")
|
877
892
|
----
|
878
893
|
|
879
894
|
==== String
|
@@ -884,7 +899,7 @@ Answers `true`/`false` based on whether string is blank, `<space>`, `\n`, `\t`,
|
|
884
899
|
|
885
900
|
[source,ruby]
|
886
901
|
----
|
887
|
-
" \n\t\r".blank?
|
902
|
+
" \n\t\r".blank? # true
|
888
903
|
----
|
889
904
|
|
890
905
|
===== #camelcase
|
@@ -893,7 +908,7 @@ Answers a camelcased string.
|
|
893
908
|
|
894
909
|
[source,ruby]
|
895
910
|
----
|
896
|
-
"this_is_an_example".camelcase
|
911
|
+
"this_is_an_example".camelcase # "ThisIsAnExample"
|
897
912
|
----
|
898
913
|
|
899
914
|
===== #down
|
@@ -902,7 +917,7 @@ Answers string with only first letter downcased.
|
|
902
917
|
|
903
918
|
[source,ruby]
|
904
919
|
----
|
905
|
-
"EXAMPLE".down
|
920
|
+
"EXAMPLE".down # "eXAMPLE"
|
906
921
|
----
|
907
922
|
|
908
923
|
===== #first
|
@@ -911,8 +926,8 @@ Answers first character of a string or first set of characters if given a number
|
|
911
926
|
|
912
927
|
[source,ruby]
|
913
928
|
----
|
914
|
-
"example".first #
|
915
|
-
"example".first 4 #
|
929
|
+
"example".first # "e"
|
930
|
+
"example".first 4 # "exam"
|
916
931
|
----
|
917
932
|
|
918
933
|
===== #indent
|
@@ -921,11 +936,11 @@ Answers string indented by two spaces by default.
|
|
921
936
|
|
922
937
|
[source,ruby]
|
923
938
|
----
|
924
|
-
"example".indent #
|
925
|
-
"example".indent 0 #
|
926
|
-
"example".indent -1 #
|
927
|
-
"example".indent 2 #
|
928
|
-
"example".indent 3, padding: " " #
|
939
|
+
"example".indent # " example"
|
940
|
+
"example".indent 0 # "example"
|
941
|
+
"example".indent -1 # "example"
|
942
|
+
"example".indent 2 # " example"
|
943
|
+
"example".indent 3, padding: " " # " example"
|
929
944
|
----
|
930
945
|
|
931
946
|
===== #last
|
@@ -934,8 +949,8 @@ Answers last character of a string or last set of characters if given a number.
|
|
934
949
|
|
935
950
|
[source,ruby]
|
936
951
|
----
|
937
|
-
"instant".last #
|
938
|
-
"instant".last 3 #
|
952
|
+
"instant".last # "t"
|
953
|
+
"instant".last 3 # "ant"
|
939
954
|
----
|
940
955
|
|
941
956
|
===== #pluralize
|
@@ -948,14 +963,14 @@ well in other languages.
|
|
948
963
|
|
949
964
|
[source,ruby]
|
950
965
|
----
|
951
|
-
"apple".pluralize "s" #
|
952
|
-
"apple".pluralize "s", count: 0 #
|
953
|
-
"apple".pluralize "s", count: 1 #
|
954
|
-
"apple".pluralize "s", count: -1 #
|
955
|
-
"apple".pluralize "s", count: 2 #
|
956
|
-
"apple".pluralize "s", count: -2 #
|
957
|
-
"cactus".pluralize "i", replace: "us" #
|
958
|
-
"cul-de-sac".pluralize "ls", replace: "l" #
|
966
|
+
"apple".pluralize "s" # apples
|
967
|
+
"apple".pluralize "s", count: 0 # apples
|
968
|
+
"apple".pluralize "s", count: 1 # apple
|
969
|
+
"apple".pluralize "s", count: -1 # apple
|
970
|
+
"apple".pluralize "s", count: 2 # apples
|
971
|
+
"apple".pluralize "s", count: -2 # apples
|
972
|
+
"cactus".pluralize "i", replace: "us" # cacti
|
973
|
+
"cul-de-sac".pluralize "ls", replace: "l" # culs-de-sac
|
959
974
|
----
|
960
975
|
|
961
976
|
===== #singularize
|
@@ -968,14 +983,14 @@ well in other languages.
|
|
968
983
|
|
969
984
|
[source,ruby]
|
970
985
|
----
|
971
|
-
"apples".singularize "s" #
|
972
|
-
"apples".singularize "s", count: 0 #
|
973
|
-
"apples".singularize "s", count: 1 #
|
974
|
-
"apples".singularize "s", count: -1 #
|
975
|
-
"apples".singularize "s", count: 2 #
|
976
|
-
"apples".singularize "s", count: -2 #
|
977
|
-
"cacti".singularize "i", replace: "us" #
|
978
|
-
"culs-de-sac".singularize "ls", replace: "l" #
|
986
|
+
"apples".singularize "s" # apple
|
987
|
+
"apples".singularize "s", count: 0 # apples
|
988
|
+
"apples".singularize "s", count: 1 # apple
|
989
|
+
"apples".singularize "s", count: -1 # apple
|
990
|
+
"apples".singularize "s", count: 2 # apples
|
991
|
+
"apples".singularize "s", count: -2 # apples
|
992
|
+
"cacti".singularize "i", replace: "us" # cactus
|
993
|
+
"culs-de-sac".singularize "ls", replace: "l" # cul-de-sac
|
979
994
|
----
|
980
995
|
|
981
996
|
===== #snakecase
|
@@ -984,7 +999,7 @@ Answers a snakecased string.
|
|
984
999
|
|
985
1000
|
[source,ruby]
|
986
1001
|
----
|
987
|
-
"ThisIsAnExample".snakecase
|
1002
|
+
"ThisIsAnExample".snakecase # "this_is_an_example"
|
988
1003
|
----
|
989
1004
|
|
990
1005
|
===== #titleize
|
@@ -993,7 +1008,7 @@ Answers titleized string.
|
|
993
1008
|
|
994
1009
|
[source,ruby]
|
995
1010
|
----
|
996
|
-
"ThisIsAnExample".titleize
|
1011
|
+
"ThisIsAnExample".titleize # "This Is An Example"
|
997
1012
|
----
|
998
1013
|
|
999
1014
|
===== #to_bool
|
@@ -1002,11 +1017,11 @@ Answers string as a boolean.
|
|
1002
1017
|
|
1003
1018
|
[source,ruby]
|
1004
1019
|
----
|
1005
|
-
"true".to_bool #
|
1006
|
-
"yes".to_bool #
|
1007
|
-
"1".to_bool #
|
1008
|
-
"".to_bool #
|
1009
|
-
"example".to_bool #
|
1020
|
+
"true".to_bool # true
|
1021
|
+
"yes".to_bool # true
|
1022
|
+
"1".to_bool # true
|
1023
|
+
"".to_bool # false
|
1024
|
+
"example".to_bool # false
|
1010
1025
|
----
|
1011
1026
|
|
1012
1027
|
===== #up
|
@@ -1015,7 +1030,7 @@ Answers string with only first letter upcased.
|
|
1015
1030
|
|
1016
1031
|
[source,ruby]
|
1017
1032
|
----
|
1018
|
-
"example".up
|
1033
|
+
"example".up # "Example"
|
1019
1034
|
----
|
1020
1035
|
|
1021
1036
|
==== String IO
|
@@ -1029,12 +1044,12 @@ Answers full string by rewinding to beginning of string and reading all content.
|
|
1029
1044
|
io = StringIO.new
|
1030
1045
|
io.write "This is a test."
|
1031
1046
|
|
1032
|
-
io.reread #
|
1033
|
-
io.reread 4 #
|
1047
|
+
io.reread # "This is a test."
|
1048
|
+
io.reread 4 # "This"
|
1034
1049
|
|
1035
1050
|
buffer = "".dup
|
1036
|
-
io.reread(buffer: buffer) #
|
1037
|
-
buffer #
|
1051
|
+
io.reread(buffer: buffer) # "This is a test."
|
1052
|
+
buffer # "This is a test."
|
1038
1053
|
----
|
1039
1054
|
|
1040
1055
|
==== Struct
|
@@ -1045,8 +1060,8 @@ Answers whether a struct was constructed with keyword or positional arguments.
|
|
1045
1060
|
|
1046
1061
|
[source,ruby]
|
1047
1062
|
----
|
1048
|
-
Struct.new(:a, keyword_init: true).keyworded? #
|
1049
|
-
Struct.new(:a).keyworded? #
|
1063
|
+
Struct.new(:a, keyword_init: true).keyworded? # true
|
1064
|
+
Struct.new(:a).keyworded? # false
|
1050
1065
|
----
|
1051
1066
|
|
1052
1067
|
===== .with_keywords
|
@@ -1057,14 +1072,14 @@ whether the struct was constructed with positional or keyword arguments.
|
|
1057
1072
|
[source,ruby]
|
1058
1073
|
----
|
1059
1074
|
Example = Struct.new :a, :b, :c
|
1060
|
-
Example.with_keywords a: 1, b: 2, c: 3 #
|
1061
|
-
Example.with_keywords a: 1 #
|
1062
|
-
Example.with_keywords c: 1 #
|
1075
|
+
Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>"
|
1076
|
+
Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>"
|
1077
|
+
Example.with_keywords c: 1 # "#<struct a=nil, b=nil, c=1>"
|
1063
1078
|
|
1064
1079
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
1065
|
-
Example.with_keywords a: 1, b: 2, c: 3 #
|
1066
|
-
Example.with_keywords a: 1 #
|
1067
|
-
Example.with_keywords c: 1 #
|
1080
|
+
Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>"
|
1081
|
+
Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>"
|
1082
|
+
Example.with_keywords c: 1 # "#<struct a=nil, b=nil, c=1>"
|
1068
1083
|
----
|
1069
1084
|
|
1070
1085
|
===== .with_positions
|
@@ -1075,12 +1090,12 @@ whether the struct was constructed with positional or keyword arguments.
|
|
1075
1090
|
[source,ruby]
|
1076
1091
|
----
|
1077
1092
|
Example = Struct.new :a, :b, :c
|
1078
|
-
Example.with_positions 1, 2, 3 #
|
1079
|
-
Example.with_positions 1 #
|
1093
|
+
Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>"
|
1094
|
+
Example.with_positions 1 # "#<struct a=1, b=nil, c=nil>"
|
1080
1095
|
|
1081
1096
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
1082
|
-
Example.with_positions 1, 2, 3 #
|
1083
|
-
Example.with_positions 1 #
|
1097
|
+
Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>"
|
1098
|
+
Example.with_positions 1 # "#<struct a=1, b=nil, c=nil>"
|
1084
1099
|
----
|
1085
1100
|
|
1086
1101
|
===== #merge
|
@@ -1091,17 +1106,17 @@ Merges multiple attributes without mutating itself.
|
|
1091
1106
|
----
|
1092
1107
|
Example = Struct.new :a, :b, :c
|
1093
1108
|
example = Example[1, 2, 3]
|
1094
|
-
example.merge a: 10 #
|
1095
|
-
example.merge a: 10, c: 30 #
|
1096
|
-
example.merge a: 10, b: 20, c: 30 #
|
1097
|
-
example #
|
1109
|
+
example.merge a: 10 # "#<struct a=10, b=2, c=3>"
|
1110
|
+
example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
|
1111
|
+
example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
|
1112
|
+
example # "#<struct a=1, b=2, c=3>"
|
1098
1113
|
|
1099
1114
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
1100
1115
|
example = Example[a: 1, b: 2, c: 3]
|
1101
|
-
example.merge a: 10 #
|
1102
|
-
example.merge a: 10, c: 30 #
|
1103
|
-
example.merge a: 10, b: 20, c: 30 #
|
1104
|
-
example #
|
1116
|
+
example.merge a: 10 # "#<struct a=10, b=2, c=3>"
|
1117
|
+
example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
|
1118
|
+
example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
|
1119
|
+
example # "#<struct a=1, b=2, c=3>"
|
1105
1120
|
----
|
1106
1121
|
|
1107
1122
|
===== #merge!
|
@@ -1112,17 +1127,17 @@ Merges multiple attributes while mutating itself.
|
|
1112
1127
|
----
|
1113
1128
|
Example = Struct.new :a, :b, :c
|
1114
1129
|
example = Example[1, 2, 3]
|
1115
|
-
example.merge! a: 10 #
|
1116
|
-
example.merge! a: 10, c: 30 #
|
1117
|
-
example.merge! a: 10, b: 20, c: 30 #
|
1118
|
-
example #
|
1130
|
+
example.merge! a: 10 # "#<struct a=10, b=2, c=3>"
|
1131
|
+
example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
|
1132
|
+
example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
|
1133
|
+
example # "#<struct a=10, b=20, c=30>"
|
1119
1134
|
|
1120
1135
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
1121
1136
|
example = Example[a: 1, b: 2, c: 3]
|
1122
|
-
example.merge! a: 10 #
|
1123
|
-
example.merge! a: 10, c: 30 #
|
1124
|
-
example.merge! a: 10, b: 20, c: 30 #
|
1125
|
-
example #
|
1137
|
+
example.merge! a: 10 # "#<struct a=10, b=2, c=3>"
|
1138
|
+
example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
|
1139
|
+
example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
|
1140
|
+
example # "#<struct a=10, b=20, c=30>"
|
1126
1141
|
----
|
1127
1142
|
|
1128
1143
|
===== #revalue
|
@@ -1138,11 +1153,11 @@ below but a keyword struct would work too.
|
|
1138
1153
|
Example = Struct.new :a, :b, :c
|
1139
1154
|
|
1140
1155
|
example = Example[1, 2, 3]
|
1141
|
-
example.revalue { |value| value * 2 } #
|
1142
|
-
example.revalue(c: 2) { |previous, current| previous + current } #
|
1143
|
-
example.revalue c: 2 #
|
1144
|
-
example.revalue #
|
1145
|
-
example #
|
1156
|
+
example.revalue { |value| value * 2 } # "#<struct a=2, b=4, c=6>"
|
1157
|
+
example.revalue(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>"
|
1158
|
+
example.revalue c: 2 # "#<struct a=1, b=2, c=3>"
|
1159
|
+
example.revalue # "#<struct a=1, b=2, c=3>"
|
1160
|
+
example # "#<struct a=1, b=2, c=3>"
|
1146
1161
|
|
1147
1162
|
----
|
1148
1163
|
|
@@ -1159,17 +1174,17 @@ keyword struct would work too.
|
|
1159
1174
|
Example = Struct.new :a, :b, :c
|
1160
1175
|
|
1161
1176
|
example = Example[1, 2, 3]
|
1162
|
-
example.revalue! { |value| value * 2 } #
|
1163
|
-
example #
|
1177
|
+
example.revalue! { |value| value * 2 } # "#<struct a=2, b=4, c=6>"
|
1178
|
+
example # "#<struct a=2, b=4, c=6>"
|
1164
1179
|
|
1165
1180
|
example = Example[1, 2, 3]
|
1166
|
-
example.revalue!(c: 2) { |previous, current| previous + current } #
|
1167
|
-
example #
|
1181
|
+
example.revalue!(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>"
|
1182
|
+
example # "#<struct a=1, b=2, c=5>"
|
1168
1183
|
|
1169
1184
|
example = Example[1, 2, 3]
|
1170
|
-
example.revalue! c: 2 #
|
1171
|
-
example.revalue! #
|
1172
|
-
example #
|
1185
|
+
example.revalue! c: 2 # "#<struct a=1, b=2, c=3>"
|
1186
|
+
example.revalue! # "#<struct a=1, b=2, c=3>"
|
1187
|
+
example # "#<struct a=1, b=2, c=3>"
|
1173
1188
|
----
|
1174
1189
|
|
1175
1190
|
==== Symbol
|
@@ -1181,10 +1196,10 @@ with public methods in order to not break encapsulation.
|
|
1181
1196
|
|
1182
1197
|
[source,ruby]
|
1183
1198
|
----
|
1184
|
-
%w[clue crow cow].map(&:tr.call("c", "b")) #
|
1185
|
-
[%w[a b c], %w[c a b]].map(&:index.call { |element| element == "b" }) #
|
1186
|
-
%w[1.outside 2.inside].map(&:sub.call(/\./) { |bullet| bullet + " " }) #
|
1187
|
-
[1, 2, 3].map(&:to_s.call) #
|
1199
|
+
%w[clue crow cow].map(&:tr.call("c", "b")) # ["blue", "brow", "bow"]
|
1200
|
+
[%w[a b c], %w[c a b]].map(&:index.call { |element| element == "b" }) # [1, 2]
|
1201
|
+
%w[1.outside 2.inside].map(&:sub.call(/\./) { |bullet| bullet + " " }) # ["1. outside", "2. inside"]
|
1202
|
+
[1, 2, 3].map(&:to_s.call) # ["1", "2", "3"]
|
1188
1203
|
----
|
1189
1204
|
|
1190
1205
|
⚠️ Use of `#call` without any arguments or block should be avoided in order to not incur extra
|
data/lib/refinements/identity.rb
CHANGED
@@ -28,8 +28,12 @@ module Refinements
|
|
28
28
|
end
|
29
29
|
|
30
30
|
refine Pathname do
|
31
|
-
def change_dir
|
32
|
-
|
31
|
+
def change_dir
|
32
|
+
if block_given?
|
33
|
+
Dir.chdir(self) { |path| yield Pathname(path) }
|
34
|
+
else
|
35
|
+
Dir.chdir self and self
|
36
|
+
end
|
33
37
|
end
|
34
38
|
|
35
39
|
def copy to
|
data/lib/refinements/strings.rb
CHANGED
@@ -16,38 +16,26 @@ module Refinements
|
|
16
16
|
.then { |parts| combine parts, :up }
|
17
17
|
end
|
18
18
|
|
19
|
-
def down
|
20
|
-
return self if empty?
|
21
|
-
|
22
|
-
first.downcase + self[1, size]
|
23
|
-
end
|
19
|
+
def down = empty? ? self : first.downcase + self[1, size]
|
24
20
|
|
25
|
-
def first
|
21
|
+
def first maximum = 0
|
26
22
|
return self if empty?
|
23
|
+
return self[0] if maximum.zero?
|
24
|
+
return "" if maximum.negative?
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
return self[0] if max.zero?
|
31
|
-
return "" if max.negative?
|
32
|
-
|
33
|
-
self[..(max - 1)]
|
26
|
+
self[..(maximum - 1)]
|
34
27
|
end
|
35
28
|
|
36
29
|
def indent multiplier = 1, padding: " "
|
37
|
-
|
38
|
-
|
39
|
-
(padding * multiplier) + self
|
30
|
+
multiplier.negative? ? self : (padding * multiplier) + self
|
40
31
|
end
|
41
32
|
|
42
|
-
def last
|
33
|
+
def last minimum = 0
|
43
34
|
return self if empty?
|
35
|
+
return self[size - 1] if minimum.zero?
|
36
|
+
return "" if minimum.negative?
|
44
37
|
|
45
|
-
|
46
|
-
|
47
|
-
return self[size - 1] if min.zero?
|
48
|
-
return "" if min.negative?
|
49
|
-
|
50
|
-
self[(min + 1)..]
|
38
|
+
self[(minimum + 1)..]
|
51
39
|
end
|
52
40
|
|
53
41
|
def pluralize(suffix, replace: /$/, count: 0) = count.abs == 1 ? self : sub(replace, suffix)
|
@@ -72,11 +60,7 @@ module Refinements
|
|
72
60
|
|
73
61
|
def to_bool = %w[true yes on t y 1].include?(downcase.strip)
|
74
62
|
|
75
|
-
def up
|
76
|
-
return self if empty?
|
77
|
-
|
78
|
-
first.upcase + self[1, size]
|
79
|
-
end
|
63
|
+
def up = empty? ? self : first.upcase + self[1, size]
|
80
64
|
|
81
65
|
private
|
82
66
|
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: refinements
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 8.5.
|
4
|
+
version: 8.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
@@ -28,7 +28,7 @@ cert_chain:
|
|
28
28
|
lkHilIrX69jq8wMPpBhlaw2mRmeSL50Wv5u6xVBvOHhXFSP1crXM95vfLhLyRYod
|
29
29
|
W2A=
|
30
30
|
-----END CERTIFICATE-----
|
31
|
-
date: 2021-10-
|
31
|
+
date: 2021-10-21 00:00:00.000000000 Z
|
32
32
|
dependencies: []
|
33
33
|
description:
|
34
34
|
email:
|
metadata.gz.sig
CHANGED
Binary file
|