refinements 8.4.1 → 9.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/LICENSE.adoc +207 -155
- data/README.adoc +342 -216
- data/lib/refinements/arrays.rb +1 -1
- data/lib/refinements/hashes.rb +8 -2
- data/lib/refinements/identity.rb +1 -1
- data/lib/refinements/ios.rb +1 -1
- data/lib/refinements/pathnames.rb +10 -2
- data/lib/refinements/strings.rb +14 -26
- data/lib/refinements/structs.rb +3 -3
- data/lib/refinements/symbols.rb +14 -0
- data/lib/refinements.rb +1 -0
- data.tar.gz.sig +0 -0
- metadata +7 -5
- metadata.gz.sig +0 -0
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_patches[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
|
|
@@ -77,6 +80,7 @@ require "refinements/pathnames"
|
|
77
80
|
require "refinements/strings"
|
78
81
|
require "refinements/string_ios"
|
79
82
|
require "refinements/structs"
|
83
|
+
require "refinements/symbols"
|
80
84
|
----
|
81
85
|
|
82
86
|
=== Using
|
@@ -96,6 +100,7 @@ class Example
|
|
96
100
|
using Refinements::Strings
|
97
101
|
using Refinements::StringIOs
|
98
102
|
using Refinements::Structs
|
103
|
+
using Refinements::Symbols
|
99
104
|
end
|
100
105
|
----
|
101
106
|
|
@@ -113,8 +118,9 @@ Removes `nil` and empty objects without mutating itself.
|
|
113
118
|
----
|
114
119
|
object = Object.new
|
115
120
|
example = [1, "blueberry", nil, "", [], {}, object]
|
116
|
-
|
117
|
-
example
|
121
|
+
|
122
|
+
example.compress # [1, "blueberry", object]
|
123
|
+
example # [1, "blueberry", nil, "", [], {}, object]
|
118
124
|
----
|
119
125
|
|
120
126
|
===== #compress!
|
@@ -125,8 +131,9 @@ Removes `nil` and empty values while mutating itself.
|
|
125
131
|
----
|
126
132
|
object = Object.new
|
127
133
|
example = [1, "blueberry", nil, "", [], {}, object]
|
128
|
-
|
129
|
-
example
|
134
|
+
|
135
|
+
example.compress # [1, "blueberry", object]
|
136
|
+
example # [1, "blueberry", object]
|
130
137
|
----
|
131
138
|
|
132
139
|
===== #excluding
|
@@ -135,8 +142,8 @@ Removes given array or elements without mutating itself.
|
|
135
142
|
|
136
143
|
[source,ruby]
|
137
144
|
----
|
138
|
-
[1, 2, 3, 4, 5].excluding [4, 5] #
|
139
|
-
[1, 2, 3, 4, 5].excluding 4, 5 #
|
145
|
+
[1, 2, 3, 4, 5].excluding [4, 5] # [1, 2, 3]
|
146
|
+
[1, 2, 3, 4, 5].excluding 4, 5 # [1, 2, 3]
|
140
147
|
----
|
141
148
|
|
142
149
|
===== #filter_find
|
@@ -151,9 +158,9 @@ handlers = [
|
|
151
158
|
->(object) { object if object == :a }
|
152
159
|
]
|
153
160
|
|
154
|
-
handlers.filter_find #
|
155
|
-
handlers.filter_find { |handler| handler.call :a } #
|
156
|
-
handlers.filter_find { |handler| handler.call :x } #
|
161
|
+
handlers.filter_find # Enumerator::Lazy
|
162
|
+
handlers.filter_find { |handler| handler.call :a } # :a
|
163
|
+
handlers.filter_find { |handler| handler.call :x } # nil
|
157
164
|
----
|
158
165
|
|
159
166
|
===== #including
|
@@ -162,8 +169,8 @@ Adds given array or elements without mutating itself.
|
|
162
169
|
|
163
170
|
[source,ruby]
|
164
171
|
----
|
165
|
-
[1, 2, 3].including [4, 5] #
|
166
|
-
[1, 2, 3].including 4, 5 #
|
172
|
+
[1, 2, 3].including [4, 5] # [1, 2, 3, 4, 5]
|
173
|
+
[1, 2, 3].including 4, 5 # [1, 2, 3, 4, 5]
|
167
174
|
----
|
168
175
|
|
169
176
|
===== #intersperse
|
@@ -172,9 +179,9 @@ Inserts additional elements or array between all members of given array.
|
|
172
179
|
|
173
180
|
[source,ruby]
|
174
181
|
----
|
175
|
-
[1, 2, 3].intersperse :a #
|
176
|
-
[1, 2, 3].intersperse :a, :b #
|
177
|
-
[1, 2, 3].intersperse %i[a b c] #
|
182
|
+
[1, 2, 3].intersperse :a # [1, :a, 2, :a, 3]
|
183
|
+
[1, 2, 3].intersperse :a, :b # [1, :a, :b, 2, :a, :b, 3]
|
184
|
+
[1, 2, 3].intersperse %i[a b c] # [1, :a, :b, :c, 2, :a, :b, :c, 3]
|
178
185
|
----
|
179
186
|
|
180
187
|
===== #maximum
|
@@ -186,8 +193,8 @@ Answers the maximum extracted value from a collection of objects.
|
|
186
193
|
Point = Struct.new :x, :y, keyword_init: true
|
187
194
|
points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
|
188
195
|
|
189
|
-
points.maximum(:x) #
|
190
|
-
points.maximum(:y) #
|
196
|
+
points.maximum(:x) # 2
|
197
|
+
points.maximum(:y) # 3
|
191
198
|
----
|
192
199
|
|
193
200
|
===== #mean
|
@@ -196,10 +203,10 @@ Answers mean/average all elements within an array.
|
|
196
203
|
|
197
204
|
[source,ruby]
|
198
205
|
----
|
199
|
-
[].mean #
|
200
|
-
[5].mean #
|
201
|
-
[1, 2, 3].mean #
|
202
|
-
[1.25, 1.5, 1.75].mean #
|
206
|
+
[].mean # 0
|
207
|
+
[5].mean # 5
|
208
|
+
[1, 2, 3].mean # 2
|
209
|
+
[1.25, 1.5, 1.75].mean # 1.5
|
203
210
|
----
|
204
211
|
|
205
212
|
===== #minimum
|
@@ -211,8 +218,8 @@ Answers the minimum extracted value from a collection of objects.
|
|
211
218
|
Point = Struct.new :x, :y, keyword_init: true
|
212
219
|
points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
|
213
220
|
|
214
|
-
points.minimum(:x) #
|
215
|
-
points.minimum(:y) #
|
221
|
+
points.minimum(:x) # 0
|
222
|
+
points.minimum(:y) # 1
|
216
223
|
----
|
217
224
|
|
218
225
|
===== #pad
|
@@ -222,9 +229,9 @@ needs to be a specific size with padded values.
|
|
222
229
|
|
223
230
|
[source,ruby]
|
224
231
|
----
|
225
|
-
[1].pad 0 #
|
226
|
-
[1].pad 0, max: 3 #
|
227
|
-
[1, 2].pad 3, max: 3 #
|
232
|
+
[1].pad 0 # [1]
|
233
|
+
[1].pad 0, max: 3 # [1, 0, 0]
|
234
|
+
[1, 2].pad 3, max: 3 # [1, 2, 3]
|
228
235
|
----
|
229
236
|
|
230
237
|
===== #ring
|
@@ -234,7 +241,7 @@ Answers a circular array which can enumerate before, current, after elements.
|
|
234
241
|
[source,ruby]
|
235
242
|
----
|
236
243
|
example = [1, 2, 3]
|
237
|
-
example.ring
|
244
|
+
example.ring # "#<Enumerator: ...>"
|
238
245
|
example.ring { |(before, current, after)| puts "#{before} #{current} #{after}" }
|
239
246
|
|
240
247
|
# [3 1 2]
|
@@ -250,7 +257,7 @@ Allows one to inspect a big decimal with numeric representation.
|
|
250
257
|
|
251
258
|
[source,ruby]
|
252
259
|
----
|
253
|
-
BigDecimal.new("5.0E-10").inspect
|
260
|
+
BigDecimal.new("5.0E-10").inspect # "#<BigDecimal:3fd3d458fe84 0.0000000005>"
|
254
261
|
----
|
255
262
|
|
256
263
|
==== DateTime
|
@@ -261,7 +268,7 @@ Answers new DateTime object for current UTC date/time.
|
|
261
268
|
|
262
269
|
[source,ruby]
|
263
270
|
----
|
264
|
-
DateTime.utc #
|
271
|
+
DateTime.utc # "#<DateTime: 2019-12-31T18:17:00+00:00 ((2458849j,65820s,181867000n),+0s,2299161j)>"
|
265
272
|
----
|
266
273
|
|
267
274
|
==== Hash
|
@@ -273,8 +280,8 @@ Answers new hash where missing keys, even deeply nested, answer an empty hash.
|
|
273
280
|
[source,ruby]
|
274
281
|
----
|
275
282
|
example = Hash.infinite
|
276
|
-
example[:a] #
|
277
|
-
example[:a][:b][:c] #
|
283
|
+
example[:a] # {}
|
284
|
+
example[:a][:b][:c] # {}
|
278
285
|
----
|
279
286
|
|
280
287
|
===== .with_default
|
@@ -284,10 +291,10 @@ Answers new hash where every top-level missing key has the same default value.
|
|
284
291
|
[source,ruby]
|
285
292
|
----
|
286
293
|
example = Hash.with_default ""
|
287
|
-
example[:a] #
|
294
|
+
example[:a] # ""
|
288
295
|
|
289
296
|
example = Hash.with_default []
|
290
|
-
example[:b] #
|
297
|
+
example[:b] # []
|
291
298
|
----
|
292
299
|
|
293
300
|
===== #compress
|
@@ -298,8 +305,9 @@ Removes `nil` and empty objects without mutating itself.
|
|
298
305
|
----
|
299
306
|
object = Object.new
|
300
307
|
example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
|
301
|
-
|
302
|
-
example
|
308
|
+
|
309
|
+
example.compress # {a: 1, b: "blueberry", g: object}
|
310
|
+
example # {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
|
303
311
|
----
|
304
312
|
|
305
313
|
===== #compress!
|
@@ -310,8 +318,9 @@ Removes `nil` and empty objects while mutating itself.
|
|
310
318
|
----
|
311
319
|
object = Object.new
|
312
320
|
example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
|
313
|
-
|
314
|
-
example
|
321
|
+
|
322
|
+
example.compress! # {a: 1, b: "blueberry", g: object}
|
323
|
+
example # {a: 1, b: "blueberry", g: object}
|
315
324
|
----
|
316
325
|
|
317
326
|
===== #deep_merge
|
@@ -321,8 +330,9 @@ Merges deeply nested hashes together without mutating itself.
|
|
321
330
|
[source,ruby]
|
322
331
|
----
|
323
332
|
example = {a: "A", b: {one: "One", two: "Two"}}
|
324
|
-
|
325
|
-
example
|
333
|
+
|
334
|
+
example.deep_merge b: {one: 1} # {a: "A", b: {one: 1, two: "Two"}}
|
335
|
+
example # {a: "A", b: {one: "One", two: "Two"}}
|
326
336
|
----
|
327
337
|
|
328
338
|
===== #deep_merge!
|
@@ -332,8 +342,9 @@ Merges deeply nested hashes together while mutating itself.
|
|
332
342
|
[source,ruby]
|
333
343
|
----
|
334
344
|
example = {a: "A", b: {one: "One", two: "Two"}}
|
335
|
-
|
336
|
-
example
|
345
|
+
|
346
|
+
example.deep_merge! b: {one: 1} # {a: "A", b: {one: 1, two: "Two"}}
|
347
|
+
example # {a: "A", b: {one: 1, two: "Two"}}
|
337
348
|
----
|
338
349
|
|
339
350
|
===== #deep_stringify_keys
|
@@ -343,8 +354,8 @@ Stringifies keys of nested hash without mutating itself. Does not handle nested
|
|
343
354
|
[source,ruby]
|
344
355
|
----
|
345
356
|
example = {a: {b: 2}}
|
346
|
-
example.deep_stringify_keys #
|
347
|
-
example #
|
357
|
+
example.deep_stringify_keys # {"a" => {"b" => 1}}
|
358
|
+
example # {a: {b: 2}}
|
348
359
|
----
|
349
360
|
|
350
361
|
===== #deep_stringify_keys!
|
@@ -354,8 +365,8 @@ Stringifies keys of nested hash while mutating itself. Does not handle nested ar
|
|
354
365
|
[source,ruby]
|
355
366
|
----
|
356
367
|
example = {a: {b: 2}}
|
357
|
-
example.deep_stringify_keys! #
|
358
|
-
example #
|
368
|
+
example.deep_stringify_keys! # {"a" => {"b" => 1}}
|
369
|
+
example # {"a" => {"b" => 1}}
|
359
370
|
----
|
360
371
|
|
361
372
|
===== #deep_symbolize_keys
|
@@ -365,8 +376,8 @@ Symbolizes keys of nested hash without mutating itself. Does not handle nested a
|
|
365
376
|
[source,ruby]
|
366
377
|
----
|
367
378
|
example = {"a" => {"b" => 2}}
|
368
|
-
example.deep_symbolize_keys #
|
369
|
-
example #
|
379
|
+
example.deep_symbolize_keys # {a: {b: 1}}
|
380
|
+
example # {"a" => {"b" => 2}}
|
370
381
|
----
|
371
382
|
|
372
383
|
===== #deep_symbolize_keys!
|
@@ -376,8 +387,24 @@ Symbolizes keys of nested hash while mutating itself. Does not handle nested arr
|
|
376
387
|
[source,ruby]
|
377
388
|
----
|
378
389
|
example = {"a" => {"b" => 2}}
|
379
|
-
example.deep_symbolize_keys! #
|
380
|
-
example #
|
390
|
+
example.deep_symbolize_keys! # {a: {b: 1}}
|
391
|
+
example # {a: {b: 1}}
|
392
|
+
----
|
393
|
+
|
394
|
+
===== #fetch_value
|
395
|
+
|
396
|
+
Fetches value for exiting or missing key. Behavior is identical to `#fetch` except when the value of
|
397
|
+
the key is `nil` you'll get the default value instead. This eliminates the need for using an _or_
|
398
|
+
expression `example.fetch(:desired_key) || "default_value"`.
|
399
|
+
|
400
|
+
[source,ruby]
|
401
|
+
----
|
402
|
+
{a: "test"}.fetch_value :a, "default" # "test"
|
403
|
+
{a: "test"}.fetch_value :a # "test"
|
404
|
+
{a: nil}.fetch_value :a, "default" # "default"
|
405
|
+
{}.fetch_value(:a) { "default" } # "default"
|
406
|
+
{}.fetch_value :a # KeyError
|
407
|
+
{a: "test"}.fetch_value # ArgumentError
|
381
408
|
----
|
382
409
|
|
383
410
|
===== #flatten_keys
|
@@ -387,15 +414,15 @@ though.
|
|
387
414
|
|
388
415
|
[source,ruby]
|
389
416
|
----
|
390
|
-
{a: {b: 1}}.flatten_keys prefix: :test
|
391
|
-
{a: {b: 1}}.flatten_keys delimiter: :|
|
417
|
+
{a: {b: 1}}.flatten_keys prefix: :test # {test_a_b: 1}
|
418
|
+
{a: {b: 1}}.flatten_keys delimiter: :| # {:"a|b" => 1}
|
392
419
|
|
393
|
-
{a: {b: 1}}.flatten_keys cast: :to_s #
|
394
|
-
{"a" => {"b" => 1}}.flatten_keys cast: :to_sym #
|
420
|
+
{a: {b: 1}}.flatten_keys cast: :to_s # {"a_b" => 1}
|
421
|
+
{"a" => {"b" => 1}}.flatten_keys cast: :to_sym # {a_b: 1}
|
395
422
|
|
396
423
|
example = {a: {b: 1}}
|
397
|
-
example.flatten_keys
|
398
|
-
example
|
424
|
+
example.flatten_keys # {a_b: 1}
|
425
|
+
example # {a: {b: 1}}
|
399
426
|
----
|
400
427
|
|
401
428
|
===== #flatten_keys!
|
@@ -406,8 +433,8 @@ though.
|
|
406
433
|
[source,ruby]
|
407
434
|
----
|
408
435
|
example = {a: {b: 1}}
|
409
|
-
example.flatten_keys! #
|
410
|
-
example #
|
436
|
+
example.flatten_keys! # {a_b: 1}
|
437
|
+
example # {a_b: 1}
|
411
438
|
----
|
412
439
|
|
413
440
|
===== #recurse
|
@@ -418,8 +445,8 @@ handle nested arrays, though.
|
|
418
445
|
[source,ruby]
|
419
446
|
----
|
420
447
|
example = {"a" => {"b" => 1}}
|
421
|
-
example.recurse(&:symbolize_keys) #
|
422
|
-
example.recurse(&:invert) #
|
448
|
+
example.recurse(&:symbolize_keys) # {a: {b: 1}}
|
449
|
+
example.recurse(&:invert) # {{"b" => 1} => "a"}
|
423
450
|
----
|
424
451
|
|
425
452
|
===== #stringify_keys
|
@@ -429,8 +456,8 @@ Converts keys to strings without mutating itself.
|
|
429
456
|
[source,ruby]
|
430
457
|
----
|
431
458
|
example = {a: 1, b: 2}
|
432
|
-
example.stringify_keys #
|
433
|
-
example #
|
459
|
+
example.stringify_keys # {"a" => 1, "b" => 2}
|
460
|
+
example # {a: 1, b: 2}
|
434
461
|
----
|
435
462
|
|
436
463
|
===== #stringify_keys!
|
@@ -440,8 +467,8 @@ Converts keys to strings while mutating itself.
|
|
440
467
|
[source,ruby]
|
441
468
|
----
|
442
469
|
example = {a: 1, b: 2}
|
443
|
-
example.stringify_keys! #
|
444
|
-
example #
|
470
|
+
example.stringify_keys! # {"a" => 1, "b" => 2}
|
471
|
+
example # {"a" => 1, "b" => 2}
|
445
472
|
----
|
446
473
|
|
447
474
|
===== #symbolize_keys
|
@@ -451,8 +478,8 @@ Converts keys to symbols without mutating itself.
|
|
451
478
|
[source,ruby]
|
452
479
|
----
|
453
480
|
example = {"a" => 1, "b" => 2}
|
454
|
-
example.symbolize_keys #
|
455
|
-
example #
|
481
|
+
example.symbolize_keys # {a: 1, b: 2}
|
482
|
+
example # {"a" => 1, "b" => 2}
|
456
483
|
----
|
457
484
|
|
458
485
|
===== #symbolize_keys!
|
@@ -462,8 +489,8 @@ Converts keys to symbols while mutating itself.
|
|
462
489
|
[source,ruby]
|
463
490
|
----
|
464
491
|
example = {"a" => 1, "b" => 2}
|
465
|
-
example.symbolize_keys! #
|
466
|
-
example #
|
492
|
+
example.symbolize_keys! # {a: 1, b: 2}
|
493
|
+
example # {a: 1, b: 2}
|
467
494
|
----
|
468
495
|
|
469
496
|
===== #use
|
@@ -473,7 +500,8 @@ Passes each hash value as a block argument for further processing.
|
|
473
500
|
[source,ruby]
|
474
501
|
----
|
475
502
|
example = {unit: "221B", street: "Baker Street", city: "London", country: "UK"}
|
476
|
-
|
503
|
+
|
504
|
+
example.use { |unit, street| "#{unit} #{street}" } # "221B Baker Street"
|
477
505
|
----
|
478
506
|
|
479
507
|
==== IO
|
@@ -486,8 +514,8 @@ block, you'll need to close the stream manually.
|
|
486
514
|
|
487
515
|
[source,ruby]
|
488
516
|
----
|
489
|
-
io = IO.void #
|
490
|
-
io = IO.void { |void| void.write "nevermore" } #
|
517
|
+
io = IO.void # "#<IO:fd 20>"
|
518
|
+
io = IO.void { |void| void.write "nevermore" } # "#<IO:(closed)>"
|
491
519
|
----
|
492
520
|
|
493
521
|
===== #redirect
|
@@ -500,8 +528,8 @@ answered instead.
|
|
500
528
|
io = IO.new IO.sysopen(Pathname("test.txt").to_s, "w+")
|
501
529
|
other = IO.new IO.sysopen(Pathname("other.txt").to_s, "w+")
|
502
530
|
|
503
|
-
io.redirect other #
|
504
|
-
io.redirect(other) { |stream| stream.write "test" } #
|
531
|
+
io.redirect other # "#<IO:fd 20>"
|
532
|
+
io.redirect(other) { |stream| stream.write "test" } # "#<IO:fd 21>"
|
505
533
|
----
|
506
534
|
|
507
535
|
===== #reread
|
@@ -513,12 +541,12 @@ Answers full stream by rewinding to beginning of stream and reading all content.
|
|
513
541
|
io = IO.new IO.sysopen(Pathname("test.txt").to_s, "w+")
|
514
542
|
io.write "This is a test."
|
515
543
|
|
516
|
-
io.reread
|
517
|
-
io.reread 4
|
544
|
+
io.reread # "This is a test."
|
545
|
+
io.reread 4 # "This"
|
518
546
|
|
519
547
|
buffer = "".dup
|
520
|
-
io.reread(buffer: buffer) #
|
521
|
-
buffer #
|
548
|
+
io.reread(buffer: buffer) # "This is a test."
|
549
|
+
buffer # "This is a test."
|
522
550
|
----
|
523
551
|
|
524
552
|
===== #squelch
|
@@ -529,9 +557,10 @@ arguments or when given a block.
|
|
529
557
|
[source,ruby]
|
530
558
|
----
|
531
559
|
io = IO.new IO.sysopen(Pathname("test.txt").to_s, "w+")
|
532
|
-
|
533
|
-
io.squelch
|
534
|
-
io.
|
560
|
+
|
561
|
+
io.squelch # "#<IO:fd 20>"
|
562
|
+
io.squelch { io.write "Test" } # "#<IO:fd 20>"
|
563
|
+
io.reread # ""
|
535
564
|
----
|
536
565
|
|
537
566
|
==== Pathname
|
@@ -545,7 +574,7 @@ construct a valid path.
|
|
545
574
|
|
546
575
|
[source,ruby]
|
547
576
|
----
|
548
|
-
Pathname(nil)
|
577
|
+
Pathname(nil) # Pathname("")
|
549
578
|
----
|
550
579
|
|
551
580
|
===== .home
|
@@ -554,7 +583,7 @@ Answers user home directory.
|
|
554
583
|
|
555
584
|
[source,ruby]
|
556
585
|
----
|
557
|
-
Pathname.home #
|
586
|
+
Pathname.home # Pathname "/Users/demo"
|
558
587
|
----
|
559
588
|
|
560
589
|
===== .make_temp_dir
|
@@ -571,13 +600,13 @@ reality, these paths will be longer depending on which operating system you are
|
|
571
600
|
|
572
601
|
[source,ruby]
|
573
602
|
----
|
574
|
-
Pathname.make_temp_dir #
|
575
|
-
Pathname.make_temp_dir prefix: "prefix-" #
|
576
|
-
Pathname.make_temp_dir suffix: "-suffix" #
|
577
|
-
Pathname.make_temp_dir prefix: "prefix-", suffix: "-suffix" #
|
578
|
-
Pathname.make_temp_dir root: "/example" #
|
579
|
-
Pathname.make_temp_dir { "I am a block result" } #
|
580
|
-
Pathname.make_temp_dir { |path| path.join "sub_dir" } #
|
603
|
+
Pathname.make_temp_dir # Pathname:/var/folders/T/temp-20200101-16940-r8
|
604
|
+
Pathname.make_temp_dir prefix: "prefix-" # Pathname:/var/folders/T/prefix-20200101-16940-r8
|
605
|
+
Pathname.make_temp_dir suffix: "-suffix" # Pathname:/var/folders/T/temp-20200101-16940-r8-suffix
|
606
|
+
Pathname.make_temp_dir prefix: "prefix-", suffix: "-suffix" # Pathname:/var/folders/T/prefix-20200101-16940-r8-suffix
|
607
|
+
Pathname.make_temp_dir root: "/example" # Pathname:/example/temp-20200101-16940-r8
|
608
|
+
Pathname.make_temp_dir { "I am a block result" } # "I am a block result"
|
609
|
+
Pathname.make_temp_dir { |path| path.join "sub_dir" } # Pathname:/var/folders/T/temp-20200101-16940-r8/sub_dir
|
581
610
|
----
|
582
611
|
|
583
612
|
===== .require_tree
|
@@ -614,7 +643,7 @@ Answers operating system root path.
|
|
614
643
|
|
615
644
|
[source,ruby]
|
616
645
|
----
|
617
|
-
Pathname.root #
|
646
|
+
Pathname.root # Pathname "/"
|
618
647
|
----
|
619
648
|
|
620
649
|
===== #change_dir
|
@@ -624,13 +653,13 @@ link:https://rubyapi.org/o/Dir.chdir#method-c-chdir[Dir.chdir] for details.
|
|
624
653
|
|
625
654
|
[source,ruby]
|
626
655
|
----
|
627
|
-
Pathname.pwd
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
Pathname.pwd
|
656
|
+
current = Pathname.pwd # "$HOME/demo" (Present Working Directory)
|
657
|
+
custom = current.join("test").make_dir # Pathname "$HOME/demo/test"
|
658
|
+
custom.change_dir # "$HOME/demo/test" (Present Working Directory)
|
659
|
+
current.change_dir # "$HOME/demo" (Present Working Directory)
|
660
|
+
custom.change_dir { "example" } # "example"
|
661
|
+
custom.change_dir { |path| path } # Pathname "$HOME/demo/test"
|
662
|
+
Pathname.pwd # "$HOME/demo" (Present Working Directory)
|
634
663
|
----
|
635
664
|
|
636
665
|
===== #copy
|
@@ -639,7 +668,18 @@ Copies file from current location to new location while answering itself so it c
|
|
639
668
|
|
640
669
|
[source,ruby]
|
641
670
|
----
|
642
|
-
Pathname("input.txt").copy Pathname("output.txt") #
|
671
|
+
Pathname("input.txt").copy Pathname("output.txt") # Pathname("input.txt")
|
672
|
+
----
|
673
|
+
|
674
|
+
===== #deep_touch
|
675
|
+
|
676
|
+
Has all of the same functionality as the `#touch` method while being able to create ancestor
|
677
|
+
directories no matter how deeply nested the file might be.
|
678
|
+
|
679
|
+
[source,ruby]
|
680
|
+
----
|
681
|
+
Pathname("a/b/c/d.txt").touch # Pathname("a/b/c/d.txt")
|
682
|
+
Pathname("a/b/c/d.txt").touch Time.now - 1 # Pathname("a/b/c/d.txt")
|
643
683
|
----
|
644
684
|
|
645
685
|
===== #delete
|
@@ -649,10 +689,10 @@ Deletes file or directory and answers itself so it can be chained.
|
|
649
689
|
[source,ruby]
|
650
690
|
----
|
651
691
|
# When path exists.
|
652
|
-
Pathname("/example.txt").touch.delete #
|
692
|
+
Pathname("/example.txt").touch.delete # Pathname("/example")
|
653
693
|
|
654
694
|
# When path doesn't exist.
|
655
|
-
Pathname("/example.txt").delete #
|
695
|
+
Pathname("/example.txt").delete # Errno::ENOENT
|
656
696
|
----
|
657
697
|
|
658
698
|
===== #directories
|
@@ -661,9 +701,23 @@ Answers all directories or filtered directories for current path.
|
|
661
701
|
|
662
702
|
[source,ruby]
|
663
703
|
----
|
664
|
-
Pathname("/example").directories #
|
665
|
-
Pathname("/example").directories "a*" #
|
666
|
-
Pathname("/example").directories flag: File::FNM_DOTMATCH #
|
704
|
+
Pathname("/example").directories # [Pathname("a"), Pathname("b")]
|
705
|
+
Pathname("/example").directories "a*" # [Pathname("a")]
|
706
|
+
Pathname("/example").directories flag: File::FNM_DOTMATCH # [Pathname(".."), Pathname(".")]
|
707
|
+
----
|
708
|
+
|
709
|
+
===== #empty
|
710
|
+
|
711
|
+
Empties a directory of children (i.e. folders, nested folders, or files) or clears an existing file
|
712
|
+
of contents. If a directory or file doesn't exist, it will be created.
|
713
|
+
|
714
|
+
[source,ruby]
|
715
|
+
----
|
716
|
+
directory = Pathname("test").make_path
|
717
|
+
file = directory.join("test.txt").write("example")
|
718
|
+
|
719
|
+
file.empty.read # ""
|
720
|
+
directory.empty.children # []
|
667
721
|
----
|
668
722
|
|
669
723
|
===== #extensions
|
@@ -672,7 +726,7 @@ Answers file extensions as an array.
|
|
672
726
|
|
673
727
|
[source,ruby]
|
674
728
|
----
|
675
|
-
Pathname("example.txt.erb").extensions #
|
729
|
+
Pathname("example.txt.erb").extensions # [".txt", ".erb"]
|
676
730
|
----
|
677
731
|
|
678
732
|
===== #files
|
@@ -681,9 +735,9 @@ Answers all files or filtered files for current path.
|
|
681
735
|
|
682
736
|
[source,ruby]
|
683
737
|
----
|
684
|
-
Pathname("/example").files #
|
685
|
-
Pathname("/example").files "*.png" #
|
686
|
-
Pathname("/example").files flag: File::FNM_DOTMATCH #
|
738
|
+
Pathname("/example").files # [Pathname("a.txt"), Pathname("a.png")]
|
739
|
+
Pathname("/example").files "*.png" # [Pathname("a.png")]
|
740
|
+
Pathname("/example").files flag: File::FNM_DOTMATCH # [Pathname(".ruby-version")]
|
687
741
|
----
|
688
742
|
|
689
743
|
===== #gsub
|
@@ -693,10 +747,10 @@ Same behavior as `String#gsub` but answers a path with patterns replaced with de
|
|
693
747
|
[source,ruby]
|
694
748
|
----
|
695
749
|
Pathname("/a/path/some/path").gsub("path", "test")
|
696
|
-
#
|
750
|
+
# Pathname("/a/test/some/test")
|
697
751
|
|
698
752
|
Pathname("/%placeholder%/some/%placeholder%").gsub("%placeholder%", "test")
|
699
|
-
#
|
753
|
+
# Pathname("/test/some/test")
|
700
754
|
----
|
701
755
|
|
702
756
|
===== #make_ancestors
|
@@ -705,9 +759,9 @@ Ensures all ancestor directories are created for a path.
|
|
705
759
|
|
706
760
|
[source,ruby]
|
707
761
|
----
|
708
|
-
Pathname("/one/two").make_ancestors #
|
709
|
-
Pathname("/one").exist? #
|
710
|
-
Pathname("/one/two").exist? #
|
762
|
+
Pathname("/one/two").make_ancestors # Pathname("/one/two")
|
763
|
+
Pathname("/one").exist? # true
|
764
|
+
Pathname("/one/two").exist? # false
|
711
765
|
----
|
712
766
|
|
713
767
|
===== #make_dir
|
@@ -717,8 +771,8 @@ not throwing errors when directory does exist in order to ensure the pathname ca
|
|
717
771
|
|
718
772
|
[source,ruby]
|
719
773
|
----
|
720
|
-
Pathname("/one").make_dir #
|
721
|
-
Pathname("/one").make_dir.make_dir #
|
774
|
+
Pathname("/one").make_dir # Pathname("/one")
|
775
|
+
Pathname("/one").make_dir.make_dir # Pathname("/one")
|
722
776
|
----
|
723
777
|
|
724
778
|
===== #make_path
|
@@ -728,8 +782,8 @@ not throwing errors when directory does exist in order to ensure the pathname ca
|
|
728
782
|
|
729
783
|
[source,ruby]
|
730
784
|
----
|
731
|
-
Pathname("/one/two/three").make_path #
|
732
|
-
Pathname("/one/two/three").make_path.make_path #
|
785
|
+
Pathname("/one/two/three").make_path # Pathname("/one/two/three")
|
786
|
+
Pathname("/one/two/three").make_path.make_path # Pathname("/one/two/three")
|
733
787
|
----
|
734
788
|
|
735
789
|
===== #name
|
@@ -738,7 +792,7 @@ Answers file name without extension.
|
|
738
792
|
|
739
793
|
[source,ruby]
|
740
794
|
----
|
741
|
-
Pathname("example.txt").name #
|
795
|
+
Pathname("example.txt").name # Pathname("example")
|
742
796
|
----
|
743
797
|
|
744
798
|
===== #relative_parent
|
@@ -747,7 +801,7 @@ Answers relative path from parent directory. This is a complement to `#relative_
|
|
747
801
|
|
748
802
|
[source,ruby]
|
749
803
|
----
|
750
|
-
Pathname("/one/two/three").relative_parent("/one")
|
804
|
+
Pathname("/one/two/three").relative_parent("/one") # Pathname "two"
|
751
805
|
----
|
752
806
|
|
753
807
|
===== #remove_dir
|
@@ -757,9 +811,9 @@ not throwing errors when directory does exist in order to ensure the pathname ca
|
|
757
811
|
|
758
812
|
[source,ruby]
|
759
813
|
----
|
760
|
-
Pathname("/test").make_dir.remove_dir.exist? #
|
761
|
-
Pathname("/test").remove_dir #
|
762
|
-
Pathname("/test").remove_dir.remove_dir #
|
814
|
+
Pathname("/test").make_dir.remove_dir.exist? # false
|
815
|
+
Pathname("/test").remove_dir # Pathname("/test")
|
816
|
+
Pathname("/test").remove_dir.remove_dir # Pathname("/test")
|
763
817
|
----
|
764
818
|
|
765
819
|
===== #remove_tree
|
@@ -773,14 +827,14 @@ parent_path = Pathname "/one"
|
|
773
827
|
child_path = parent_path.join "two"
|
774
828
|
|
775
829
|
child_path.make_path
|
776
|
-
child_path.remove_tree
|
777
|
-
child_path.exist?
|
778
|
-
paremt_path.exist?
|
830
|
+
child_path.remove_tree # Pathname "/one/two"
|
831
|
+
child_path.exist? # false
|
832
|
+
paremt_path.exist? # true
|
779
833
|
|
780
834
|
child_path.make_path
|
781
|
-
parent_path.remove_tree #
|
782
|
-
child_path.exist? #
|
783
|
-
parent_path.exist? #
|
835
|
+
parent_path.remove_tree # Pathname "/one"
|
836
|
+
child_path.exist? # false
|
837
|
+
parent_path.exist? # false
|
784
838
|
----
|
785
839
|
|
786
840
|
===== #rewrite
|
@@ -790,18 +844,21 @@ immediate writing back to the same file.
|
|
790
844
|
|
791
845
|
[source,ruby]
|
792
846
|
----
|
793
|
-
Pathname("/test.txt").rewrite #
|
794
|
-
Pathname("/test.txt").rewrite { |body| body.sub "[token]", "example" } #
|
847
|
+
Pathname("/test.txt").rewrite # Pathname("/test.txt")
|
848
|
+
Pathname("/test.txt").rewrite { |body| body.sub "[token]", "example" } # Pathname("/test.txt")
|
795
849
|
----
|
796
850
|
|
797
851
|
===== #touch
|
798
852
|
|
799
|
-
Updates access and modification times for path
|
853
|
+
Updates access and modification times for an existing path by defaulting to current time. When path
|
854
|
+
doesn't exist, it will be created as a file.
|
800
855
|
|
801
856
|
[source,ruby]
|
802
857
|
----
|
803
|
-
Pathname("example
|
804
|
-
Pathname("example
|
858
|
+
Pathname("example").touch # Pathname("example")
|
859
|
+
Pathname("example").touch Time.now - 1 # Pathname("example")
|
860
|
+
Pathname("example.txt").touch # Pathname("example.txt")
|
861
|
+
Pathname("example.txt").touch Time.now - 1 # Pathname("example.txt")
|
805
862
|
----
|
806
863
|
|
807
864
|
===== #write
|
@@ -811,9 +868,9 @@ options.
|
|
811
868
|
|
812
869
|
[source,ruby]
|
813
870
|
----
|
814
|
-
Pathname("example.txt").write "test" #
|
815
|
-
Pathname("example.txt").write "test", offset: 1 #
|
816
|
-
Pathname("example.txt").write "test", mode: "a" #
|
871
|
+
Pathname("example.txt").write "test" # Pathname("example.txt")
|
872
|
+
Pathname("example.txt").write "test", offset: 1 # Pathname("example.txt")
|
873
|
+
Pathname("example.txt").write "test", mode: "a" # Pathname("example.txt")
|
817
874
|
----
|
818
875
|
|
819
876
|
==== String
|
@@ -824,7 +881,7 @@ Answers `true`/`false` based on whether string is blank, `<space>`, `\n`, `\t`,
|
|
824
881
|
|
825
882
|
[source,ruby]
|
826
883
|
----
|
827
|
-
" \n\t\r".blank?
|
884
|
+
" \n\t\r".blank? # true
|
828
885
|
----
|
829
886
|
|
830
887
|
===== #camelcase
|
@@ -833,7 +890,7 @@ Answers a camelcased string.
|
|
833
890
|
|
834
891
|
[source,ruby]
|
835
892
|
----
|
836
|
-
"this_is_an_example".camelcase
|
893
|
+
"this_is_an_example".camelcase # "ThisIsAnExample"
|
837
894
|
----
|
838
895
|
|
839
896
|
===== #down
|
@@ -842,7 +899,7 @@ Answers string with only first letter downcased.
|
|
842
899
|
|
843
900
|
[source,ruby]
|
844
901
|
----
|
845
|
-
"EXAMPLE".down
|
902
|
+
"EXAMPLE".down # "eXAMPLE"
|
846
903
|
----
|
847
904
|
|
848
905
|
===== #first
|
@@ -851,8 +908,8 @@ Answers first character of a string or first set of characters if given a number
|
|
851
908
|
|
852
909
|
[source,ruby]
|
853
910
|
----
|
854
|
-
"example".first #
|
855
|
-
"example".first 4 #
|
911
|
+
"example".first # "e"
|
912
|
+
"example".first 4 # "exam"
|
856
913
|
----
|
857
914
|
|
858
915
|
===== #indent
|
@@ -861,11 +918,11 @@ Answers string indented by two spaces by default.
|
|
861
918
|
|
862
919
|
[source,ruby]
|
863
920
|
----
|
864
|
-
"example".indent #
|
865
|
-
"example".indent 0 #
|
866
|
-
"example".indent -1 #
|
867
|
-
"example".indent 2 #
|
868
|
-
"example".indent 3, padding: " " #
|
921
|
+
"example".indent # " example"
|
922
|
+
"example".indent 0 # "example"
|
923
|
+
"example".indent -1 # "example"
|
924
|
+
"example".indent 2 # " example"
|
925
|
+
"example".indent 3, padding: " " # " example"
|
869
926
|
----
|
870
927
|
|
871
928
|
===== #last
|
@@ -874,8 +931,48 @@ Answers last character of a string or last set of characters if given a number.
|
|
874
931
|
|
875
932
|
[source,ruby]
|
876
933
|
----
|
877
|
-
"instant".last #
|
878
|
-
"instant".last 3 #
|
934
|
+
"instant".last # "t"
|
935
|
+
"instant".last 3 # "ant"
|
936
|
+
----
|
937
|
+
|
938
|
+
===== #pluralize
|
939
|
+
|
940
|
+
Answers plural form of self when given a suffix to add. The plural form of the word can be
|
941
|
+
dynamically calculated when given a count and a replacement pattern (i.e. string or regular
|
942
|
+
expression) can be supplied for further specificity. Usage is based on
|
943
|
+
link:https://en.wikipedia.org/wiki/English_plurals[plurals in English] which may or may not work
|
944
|
+
well in other languages.
|
945
|
+
|
946
|
+
[source,ruby]
|
947
|
+
----
|
948
|
+
"apple".pluralize "s" # apples
|
949
|
+
"apple".pluralize "s", count: 0 # apples
|
950
|
+
"apple".pluralize "s", count: 1 # apple
|
951
|
+
"apple".pluralize "s", count: -1 # apple
|
952
|
+
"apple".pluralize "s", count: 2 # apples
|
953
|
+
"apple".pluralize "s", count: -2 # apples
|
954
|
+
"cactus".pluralize "i", replace: "us" # cacti
|
955
|
+
"cul-de-sac".pluralize "ls", replace: "l" # culs-de-sac
|
956
|
+
----
|
957
|
+
|
958
|
+
===== #singularize
|
959
|
+
|
960
|
+
Answers singular form of self when given a suffix to remove (can be a string or a regular
|
961
|
+
expression). The singular form of the word can be dynamically calculated when given a count and a
|
962
|
+
replacement string can be supplied for further specificity. Usage is based on
|
963
|
+
link:https://en.wikipedia.org/wiki/English_plurals[plurals in English] which may or may not work
|
964
|
+
well in other languages.
|
965
|
+
|
966
|
+
[source,ruby]
|
967
|
+
----
|
968
|
+
"apples".singularize "s" # apple
|
969
|
+
"apples".singularize "s", count: 0 # apples
|
970
|
+
"apples".singularize "s", count: 1 # apple
|
971
|
+
"apples".singularize "s", count: -1 # apple
|
972
|
+
"apples".singularize "s", count: 2 # apples
|
973
|
+
"apples".singularize "s", count: -2 # apples
|
974
|
+
"cacti".singularize "i", replace: "us" # cactus
|
975
|
+
"culs-de-sac".singularize "ls", replace: "l" # cul-de-sac
|
879
976
|
----
|
880
977
|
|
881
978
|
===== #snakecase
|
@@ -884,7 +981,7 @@ Answers a snakecased string.
|
|
884
981
|
|
885
982
|
[source,ruby]
|
886
983
|
----
|
887
|
-
"ThisIsAnExample".snakecase
|
984
|
+
"ThisIsAnExample".snakecase # "this_is_an_example"
|
888
985
|
----
|
889
986
|
|
890
987
|
===== #titleize
|
@@ -893,7 +990,7 @@ Answers titleized string.
|
|
893
990
|
|
894
991
|
[source,ruby]
|
895
992
|
----
|
896
|
-
"ThisIsAnExample".titleize
|
993
|
+
"ThisIsAnExample".titleize # "This Is An Example"
|
897
994
|
----
|
898
995
|
|
899
996
|
===== #to_bool
|
@@ -902,11 +999,11 @@ Answers string as a boolean.
|
|
902
999
|
|
903
1000
|
[source,ruby]
|
904
1001
|
----
|
905
|
-
"true".to_bool #
|
906
|
-
"yes".to_bool #
|
907
|
-
"1".to_bool #
|
908
|
-
"".to_bool #
|
909
|
-
"example".to_bool #
|
1002
|
+
"true".to_bool # true
|
1003
|
+
"yes".to_bool # true
|
1004
|
+
"1".to_bool # true
|
1005
|
+
"".to_bool # false
|
1006
|
+
"example".to_bool # false
|
910
1007
|
----
|
911
1008
|
|
912
1009
|
===== #up
|
@@ -915,7 +1012,7 @@ Answers string with only first letter upcased.
|
|
915
1012
|
|
916
1013
|
[source,ruby]
|
917
1014
|
----
|
918
|
-
"example".up
|
1015
|
+
"example".up # "Example"
|
919
1016
|
----
|
920
1017
|
|
921
1018
|
==== String IO
|
@@ -929,12 +1026,12 @@ Answers full string by rewinding to beginning of string and reading all content.
|
|
929
1026
|
io = StringIO.new
|
930
1027
|
io.write "This is a test."
|
931
1028
|
|
932
|
-
io.reread #
|
933
|
-
io.reread 4 #
|
1029
|
+
io.reread # "This is a test."
|
1030
|
+
io.reread 4 # "This"
|
934
1031
|
|
935
1032
|
buffer = "".dup
|
936
|
-
io.reread(buffer: buffer) #
|
937
|
-
buffer #
|
1033
|
+
io.reread(buffer: buffer) # "This is a test."
|
1034
|
+
buffer # "This is a test."
|
938
1035
|
----
|
939
1036
|
|
940
1037
|
==== Struct
|
@@ -945,8 +1042,8 @@ Answers whether a struct was constructed with keyword or positional arguments.
|
|
945
1042
|
|
946
1043
|
[source,ruby]
|
947
1044
|
----
|
948
|
-
Struct.new(:a, keyword_init: true).keyworded? #
|
949
|
-
Struct.new(:a).keyworded? #
|
1045
|
+
Struct.new(:a, keyword_init: true).keyworded? # true
|
1046
|
+
Struct.new(:a).keyworded? # false
|
950
1047
|
----
|
951
1048
|
|
952
1049
|
===== .with_keywords
|
@@ -957,14 +1054,14 @@ whether the struct was constructed with positional or keyword arguments.
|
|
957
1054
|
[source,ruby]
|
958
1055
|
----
|
959
1056
|
Example = Struct.new :a, :b, :c
|
960
|
-
Example.with_keywords a: 1, b: 2, c: 3 #
|
961
|
-
Example.with_keywords a: 1 #
|
962
|
-
Example.with_keywords c: 1 #
|
1057
|
+
Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>"
|
1058
|
+
Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>"
|
1059
|
+
Example.with_keywords c: 1 # "#<struct a=nil, b=nil, c=1>"
|
963
1060
|
|
964
1061
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
965
|
-
Example.with_keywords a: 1, b: 2, c: 3 #
|
966
|
-
Example.with_keywords a: 1 #
|
967
|
-
Example.with_keywords c: 1 #
|
1062
|
+
Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>"
|
1063
|
+
Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>"
|
1064
|
+
Example.with_keywords c: 1 # "#<struct a=nil, b=nil, c=1>"
|
968
1065
|
----
|
969
1066
|
|
970
1067
|
===== .with_positions
|
@@ -975,54 +1072,62 @@ whether the struct was constructed with positional or keyword arguments.
|
|
975
1072
|
[source,ruby]
|
976
1073
|
----
|
977
1074
|
Example = Struct.new :a, :b, :c
|
978
|
-
Example.with_positions 1, 2, 3 #
|
979
|
-
Example.with_positions 1 #
|
1075
|
+
Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>"
|
1076
|
+
Example.with_positions 1 # "#<struct a=1, b=nil, c=nil>"
|
980
1077
|
|
981
1078
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
982
|
-
Example.with_positions 1, 2, 3 #
|
983
|
-
Example.with_positions 1 #
|
1079
|
+
Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>"
|
1080
|
+
Example.with_positions 1 # "#<struct a=1, b=nil, c=nil>"
|
984
1081
|
----
|
985
1082
|
|
986
1083
|
===== #merge
|
987
1084
|
|
988
|
-
Merges multiple attributes without mutating itself
|
1085
|
+
Merges multiple attributes without mutating itself and supports any object that responds to `#to_h`.
|
989
1086
|
|
990
1087
|
[source,ruby]
|
991
1088
|
----
|
992
|
-
|
993
|
-
example = Example[1, 2, 3]
|
994
|
-
example.merge a: 10 # => #<struct a=10, b=2, c=3>
|
995
|
-
example.merge a: 10, c: 30 # => #<struct a=10, b=2, c=30>
|
996
|
-
example.merge a: 10, b: 20, c: 30 # => #<struct a=10, b=20, c=30>
|
997
|
-
example # => #<struct a=1, b=2, c=3>
|
1089
|
+
other = Struct.new("Other", :a, :b, :c).new 7, 8, 9
|
998
1090
|
|
999
|
-
|
1000
|
-
example
|
1001
|
-
example.merge a: 10
|
1002
|
-
example.merge a: 10, c: 30
|
1003
|
-
example.merge a: 10, b: 20
|
1004
|
-
example
|
1091
|
+
example = Struct.new(:a, :b, :c).new 1, 2, 3
|
1092
|
+
example.merge a: 10 # "#<struct a=10, b=2, c=3>"
|
1093
|
+
example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
|
1094
|
+
example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
|
1095
|
+
example.merge {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
|
1096
|
+
example.merge other # "#<struct a=7, b=8, c=9>"
|
1097
|
+
example # "#<struct a=1, b=2, c=3>"
|
1098
|
+
|
1099
|
+
example = Struct.new(:a, :b, :c, keyword_init: true).new a: 1, b: 2, c: 3
|
1100
|
+
example.merge a: 10 # "#<struct a=10, b=2, c=3>"
|
1101
|
+
example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
|
1102
|
+
example.merge {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
|
1103
|
+
example.merge other # "#<struct a=7, b=8, c=9>"
|
1104
|
+
example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
|
1105
|
+
example # "#<struct a=1, b=2, c=3>"
|
1005
1106
|
----
|
1006
1107
|
|
1007
1108
|
===== #merge!
|
1008
1109
|
|
1009
|
-
Merges multiple attributes while mutating itself
|
1110
|
+
Merges multiple attributes while mutating itself and supports any object that responds to `#to_h`.
|
1010
1111
|
|
1011
1112
|
[source,ruby]
|
1012
1113
|
----
|
1013
|
-
|
1014
|
-
example = Example[1, 2, 3]
|
1015
|
-
example.merge! a: 10 # => #<struct a=10, b=2, c=3>
|
1016
|
-
example.merge! a: 10, c: 30 # => #<struct a=10, b=2, c=30>
|
1017
|
-
example.merge! a: 10, b: 20, c: 30 # => #<struct a=10, b=20, c=30>
|
1018
|
-
example # => #<struct a=10, b=20, c=30>
|
1114
|
+
other = Struct.new("Other", :a, :b, :c).new 7, 8, 9
|
1019
1115
|
|
1020
|
-
|
1021
|
-
example
|
1022
|
-
example.merge! a: 10
|
1023
|
-
example.merge! a: 10,
|
1024
|
-
example.merge!
|
1025
|
-
example
|
1116
|
+
example = Struct.new(:a, :b, :c).new 1, 2, 3
|
1117
|
+
example.merge! a: 10 # "#<struct a=10, b=2, c=3>"
|
1118
|
+
example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
|
1119
|
+
example.merge! {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
|
1120
|
+
example.merge! other # "#<struct a=7, b=8, c=9>"
|
1121
|
+
example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
|
1122
|
+
example # "#<struct a=10, b=20, c=30>"
|
1123
|
+
|
1124
|
+
example = Struct.new(:a, :b, :c, keyword_init: true).new a: 1, b: 2, c: 3
|
1125
|
+
example.merge! a: 10 # "#<struct a=10, b=2, c=3>"
|
1126
|
+
example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>"
|
1127
|
+
example.merge! {a: 10, b: 20} # "#<struct a=10, b=20, c=3>"
|
1128
|
+
example.merge! other # "#<struct a=7, b=8, c=9>"
|
1129
|
+
example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>"
|
1130
|
+
example # "#<struct a=10, b=20, c=30>"
|
1026
1131
|
----
|
1027
1132
|
|
1028
1133
|
===== #revalue
|
@@ -1035,15 +1140,12 @@ below but a keyword struct would work too.
|
|
1035
1140
|
|
1036
1141
|
[source,ruby]
|
1037
1142
|
----
|
1038
|
-
|
1039
|
-
|
1040
|
-
example =
|
1041
|
-
example.revalue
|
1042
|
-
example.revalue
|
1043
|
-
example
|
1044
|
-
example.revalue # => #<struct a=1, b=2, c=3>
|
1045
|
-
example # => #<struct a=1, b=2, c=3>
|
1046
|
-
|
1143
|
+
example = Struct.new("Example", :a, :b, :c).new 1, 2, 3
|
1144
|
+
example.revalue { |value| value * 2 } # "#<struct a=2, b=4, c=6>"
|
1145
|
+
example.revalue(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>"
|
1146
|
+
example.revalue c: 2 # "#<struct a=1, b=2, c=3>"
|
1147
|
+
example.revalue # "#<struct a=1, b=2, c=3>"
|
1148
|
+
example # "#<struct a=1, b=2, c=3>"
|
1047
1149
|
----
|
1048
1150
|
|
1049
1151
|
===== #revalue!
|
@@ -1059,19 +1161,37 @@ keyword struct would work too.
|
|
1059
1161
|
Example = Struct.new :a, :b, :c
|
1060
1162
|
|
1061
1163
|
example = Example[1, 2, 3]
|
1062
|
-
example.revalue! { |value| value * 2 } #
|
1063
|
-
example #
|
1164
|
+
example.revalue! { |value| value * 2 } # "#<struct a=2, b=4, c=6>"
|
1165
|
+
example # "#<struct a=2, b=4, c=6>"
|
1064
1166
|
|
1065
1167
|
example = Example[1, 2, 3]
|
1066
|
-
example.revalue!(c: 2) { |previous, current| previous + current } #
|
1067
|
-
example #
|
1168
|
+
example.revalue!(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>"
|
1169
|
+
example # "#<struct a=1, b=2, c=5>"
|
1068
1170
|
|
1069
1171
|
example = Example[1, 2, 3]
|
1070
|
-
example.revalue! c: 2 #
|
1071
|
-
example.revalue! #
|
1072
|
-
example #
|
1172
|
+
example.revalue! c: 2 # "#<struct a=1, b=2, c=3>"
|
1173
|
+
example.revalue! # "#<struct a=1, b=2, c=3>"
|
1174
|
+
example # "#<struct a=1, b=2, c=3>"
|
1073
1175
|
----
|
1074
1176
|
|
1177
|
+
==== Symbol
|
1178
|
+
|
1179
|
+
===== #call
|
1180
|
+
|
1181
|
+
Enhances symbol-to-proc by allowing you to send additional arguments and/or a block. This only works
|
1182
|
+
with public methods in order to not break encapsulation.
|
1183
|
+
|
1184
|
+
[source,ruby]
|
1185
|
+
----
|
1186
|
+
%w[clue crow cow].map(&:tr.call("c", "b")) # ["blue", "brow", "bow"]
|
1187
|
+
[%w[a b c], %w[c a b]].map(&:index.call { |element| element == "b" }) # [1, 2]
|
1188
|
+
%w[1.outside 2.inside].map(&:sub.call(/\./) { |bullet| bullet + " " }) # ["1. outside", "2. inside"]
|
1189
|
+
[1, 2, 3].map(&:to_s.call) # ["1", "2", "3"]
|
1190
|
+
----
|
1191
|
+
|
1192
|
+
⚠️ Use of `#call` without any arguments or block should be avoided in order to not incur extra
|
1193
|
+
processing costs since the original symbol-to-proc call can used instead.
|
1194
|
+
|
1075
1195
|
== Development
|
1076
1196
|
|
1077
1197
|
To contribute, run:
|
@@ -1116,14 +1236,20 @@ participating in this project you agree to abide by its terms.
|
|
1116
1236
|
|
1117
1237
|
Read link:CONTRIBUTING.adoc[CONTRIBUTING] for details.
|
1118
1238
|
|
1239
|
+
== Community
|
1240
|
+
|
1241
|
+
Feel free to link:https://www.alchemists.io/community[join the commmunity] for discussions related
|
1242
|
+
to this project and much more.
|
1243
|
+
|
1119
1244
|
== License
|
1120
1245
|
|
1121
1246
|
Read link:LICENSE.adoc[LICENSE] for details.
|
1122
1247
|
|
1123
|
-
==
|
1248
|
+
== Changes
|
1124
1249
|
|
1125
1250
|
Read link:CHANGES.adoc[CHANGES] for details.
|
1126
1251
|
|
1127
1252
|
== Credits
|
1128
1253
|
|
1129
|
-
|
1254
|
+
* Built with link:https://www.alchemists.io/projects/gemsmith[Gemsmith].
|
1255
|
+
* Engineered by link:https://www.alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].
|