refinements 8.2.2 → 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 +369 -197
- data/lib/refinements/arrays.rb +6 -2
- data/lib/refinements/big_decimals.rb +1 -0
- data/lib/refinements/classes.rb +13 -0
- data/lib/refinements/date_times.rb +1 -0
- data/lib/refinements/hashes.rb +17 -4
- data/lib/refinements/identity.rb +1 -1
- data/lib/refinements/ios.rb +1 -0
- data/lib/refinements/pathnames.rb +14 -4
- data/lib/refinements/string_ios.rb +1 -0
- data/lib/refinements/strings.rb +16 -27
- data/lib/refinements/structs.rb +1 -0
- data/lib/refinements/symbols.rb +12 -0
- data/lib/refinements.rb +2 -0
- data.tar.gz.sig +0 -0
- metadata +5 -3
- 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_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
|
|
|
@@ -107,13 +114,15 @@ The following sections demonstrate how each refinement enriches your objects wit
|
|
|
107
114
|
|
|
108
115
|
===== #compress
|
|
109
116
|
|
|
110
|
-
Removes `nil` and empty
|
|
117
|
+
Removes `nil` and empty objects without mutating itself.
|
|
111
118
|
|
|
112
119
|
[source,ruby]
|
|
113
120
|
----
|
|
114
|
-
|
|
115
|
-
example
|
|
116
|
-
|
|
121
|
+
object = Object.new
|
|
122
|
+
example = [1, "blueberry", nil, "", [], {}, object]
|
|
123
|
+
|
|
124
|
+
example.compress # [1, "blueberry", object]
|
|
125
|
+
example # [1, "blueberry", nil, "", [], {}, object]
|
|
117
126
|
----
|
|
118
127
|
|
|
119
128
|
===== #compress!
|
|
@@ -122,9 +131,11 @@ Removes `nil` and empty values while mutating itself.
|
|
|
122
131
|
|
|
123
132
|
[source,ruby]
|
|
124
133
|
----
|
|
125
|
-
|
|
126
|
-
example
|
|
127
|
-
|
|
134
|
+
object = Object.new
|
|
135
|
+
example = [1, "blueberry", nil, "", [], {}, object]
|
|
136
|
+
|
|
137
|
+
example.compress # [1, "blueberry", object]
|
|
138
|
+
example # [1, "blueberry", object]
|
|
128
139
|
----
|
|
129
140
|
|
|
130
141
|
===== #excluding
|
|
@@ -133,8 +144,8 @@ Removes given array or elements without mutating itself.
|
|
|
133
144
|
|
|
134
145
|
[source,ruby]
|
|
135
146
|
----
|
|
136
|
-
[1, 2, 3, 4, 5].excluding [4, 5] #
|
|
137
|
-
[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]
|
|
138
149
|
----
|
|
139
150
|
|
|
140
151
|
===== #filter_find
|
|
@@ -149,9 +160,9 @@ handlers = [
|
|
|
149
160
|
->(object) { object if object == :a }
|
|
150
161
|
]
|
|
151
162
|
|
|
152
|
-
handlers.filter_find #
|
|
153
|
-
handlers.filter_find { |handler| handler.call :a } #
|
|
154
|
-
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
|
|
155
166
|
----
|
|
156
167
|
|
|
157
168
|
===== #including
|
|
@@ -160,8 +171,8 @@ Adds given array or elements without mutating itself.
|
|
|
160
171
|
|
|
161
172
|
[source,ruby]
|
|
162
173
|
----
|
|
163
|
-
[1, 2, 3].including [4, 5] #
|
|
164
|
-
[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]
|
|
165
176
|
----
|
|
166
177
|
|
|
167
178
|
===== #intersperse
|
|
@@ -170,9 +181,9 @@ Inserts additional elements or array between all members of given array.
|
|
|
170
181
|
|
|
171
182
|
[source,ruby]
|
|
172
183
|
----
|
|
173
|
-
[1, 2, 3].intersperse :a #
|
|
174
|
-
[1, 2, 3].intersperse :a, :b #
|
|
175
|
-
[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]
|
|
176
187
|
----
|
|
177
188
|
|
|
178
189
|
===== #maximum
|
|
@@ -184,8 +195,8 @@ Answers the maximum extracted value from a collection of objects.
|
|
|
184
195
|
Point = Struct.new :x, :y, keyword_init: true
|
|
185
196
|
points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
|
|
186
197
|
|
|
187
|
-
points.maximum(:x) #
|
|
188
|
-
points.maximum(:y) #
|
|
198
|
+
points.maximum(:x) # 2
|
|
199
|
+
points.maximum(:y) # 3
|
|
189
200
|
----
|
|
190
201
|
|
|
191
202
|
===== #mean
|
|
@@ -194,10 +205,10 @@ Answers mean/average all elements within an array.
|
|
|
194
205
|
|
|
195
206
|
[source,ruby]
|
|
196
207
|
----
|
|
197
|
-
[].mean #
|
|
198
|
-
[5].mean #
|
|
199
|
-
[1, 2, 3].mean #
|
|
200
|
-
[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
|
|
201
212
|
----
|
|
202
213
|
|
|
203
214
|
===== #minimum
|
|
@@ -209,8 +220,8 @@ Answers the minimum extracted value from a collection of objects.
|
|
|
209
220
|
Point = Struct.new :x, :y, keyword_init: true
|
|
210
221
|
points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
|
|
211
222
|
|
|
212
|
-
points.minimum(:x) #
|
|
213
|
-
points.minimum(:y) #
|
|
223
|
+
points.minimum(:x) # 0
|
|
224
|
+
points.minimum(:y) # 1
|
|
214
225
|
----
|
|
215
226
|
|
|
216
227
|
===== #pad
|
|
@@ -220,9 +231,9 @@ needs to be a specific size with padded values.
|
|
|
220
231
|
|
|
221
232
|
[source,ruby]
|
|
222
233
|
----
|
|
223
|
-
[1].pad 0 #
|
|
224
|
-
[1].pad 0, max: 3 #
|
|
225
|
-
[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]
|
|
226
237
|
----
|
|
227
238
|
|
|
228
239
|
===== #ring
|
|
@@ -232,7 +243,7 @@ Answers a circular array which can enumerate before, current, after elements.
|
|
|
232
243
|
[source,ruby]
|
|
233
244
|
----
|
|
234
245
|
example = [1, 2, 3]
|
|
235
|
-
example.ring
|
|
246
|
+
example.ring # "#<Enumerator: ...>"
|
|
236
247
|
example.ring { |(before, current, after)| puts "#{before} #{current} #{after}" }
|
|
237
248
|
|
|
238
249
|
# [3 1 2]
|
|
@@ -248,7 +259,23 @@ Allows one to inspect a big decimal with numeric representation.
|
|
|
248
259
|
|
|
249
260
|
[source,ruby]
|
|
250
261
|
----
|
|
251
|
-
BigDecimal.new("5.0E-10").inspect
|
|
262
|
+
BigDecimal.new("5.0E-10").inspect # "#<BigDecimal:3fd3d458fe84 0.0000000005>"
|
|
263
|
+
----
|
|
264
|
+
|
|
265
|
+
==== Class
|
|
266
|
+
|
|
267
|
+
===== #descendants
|
|
268
|
+
|
|
269
|
+
Answers descendants of a class.
|
|
270
|
+
|
|
271
|
+
[source,ruby]
|
|
272
|
+
----
|
|
273
|
+
a = Class.new
|
|
274
|
+
b = Class.new a
|
|
275
|
+
c = Class.new a
|
|
276
|
+
|
|
277
|
+
a.descendants # [b, c]
|
|
278
|
+
Class.new.descendants # []
|
|
252
279
|
----
|
|
253
280
|
|
|
254
281
|
==== DateTime
|
|
@@ -259,7 +286,7 @@ Answers new DateTime object for current UTC date/time.
|
|
|
259
286
|
|
|
260
287
|
[source,ruby]
|
|
261
288
|
----
|
|
262
|
-
DateTime.utc #
|
|
289
|
+
DateTime.utc # "#<DateTime: 2019-12-31T18:17:00+00:00 ((2458849j,65820s,181867000n),+0s,2299161j)>"
|
|
263
290
|
----
|
|
264
291
|
|
|
265
292
|
==== Hash
|
|
@@ -271,8 +298,8 @@ Answers new hash where missing keys, even deeply nested, answer an empty hash.
|
|
|
271
298
|
[source,ruby]
|
|
272
299
|
----
|
|
273
300
|
example = Hash.infinite
|
|
274
|
-
example[:a] #
|
|
275
|
-
example[:a][:b][:c] #
|
|
301
|
+
example[:a] # {}
|
|
302
|
+
example[:a][:b][:c] # {}
|
|
276
303
|
----
|
|
277
304
|
|
|
278
305
|
===== .with_default
|
|
@@ -282,10 +309,36 @@ Answers new hash where every top-level missing key has the same default value.
|
|
|
282
309
|
[source,ruby]
|
|
283
310
|
----
|
|
284
311
|
example = Hash.with_default ""
|
|
285
|
-
example[:a] #
|
|
312
|
+
example[:a] # ""
|
|
286
313
|
|
|
287
314
|
example = Hash.with_default []
|
|
288
|
-
example[:b] #
|
|
315
|
+
example[:b] # []
|
|
316
|
+
----
|
|
317
|
+
|
|
318
|
+
===== #compress
|
|
319
|
+
|
|
320
|
+
Removes `nil` and empty objects without mutating itself.
|
|
321
|
+
|
|
322
|
+
[source,ruby]
|
|
323
|
+
----
|
|
324
|
+
object = Object.new
|
|
325
|
+
example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
|
|
326
|
+
|
|
327
|
+
example.compress # {a: 1, b: "blueberry", g: object}
|
|
328
|
+
example # {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
|
|
329
|
+
----
|
|
330
|
+
|
|
331
|
+
===== #compress!
|
|
332
|
+
|
|
333
|
+
Removes `nil` and empty objects while mutating itself.
|
|
334
|
+
|
|
335
|
+
[source,ruby]
|
|
336
|
+
----
|
|
337
|
+
object = Object.new
|
|
338
|
+
example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
|
|
339
|
+
|
|
340
|
+
example.compress! # {a: 1, b: "blueberry", g: object}
|
|
341
|
+
example # {a: 1, b: "blueberry", g: object}
|
|
289
342
|
----
|
|
290
343
|
|
|
291
344
|
===== #deep_merge
|
|
@@ -295,8 +348,9 @@ Merges deeply nested hashes together without mutating itself.
|
|
|
295
348
|
[source,ruby]
|
|
296
349
|
----
|
|
297
350
|
example = {a: "A", b: {one: "One", two: "Two"}}
|
|
298
|
-
|
|
299
|
-
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"}}
|
|
300
354
|
----
|
|
301
355
|
|
|
302
356
|
===== #deep_merge!
|
|
@@ -306,8 +360,9 @@ Merges deeply nested hashes together while mutating itself.
|
|
|
306
360
|
[source,ruby]
|
|
307
361
|
----
|
|
308
362
|
example = {a: "A", b: {one: "One", two: "Two"}}
|
|
309
|
-
|
|
310
|
-
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"}}
|
|
311
366
|
----
|
|
312
367
|
|
|
313
368
|
===== #deep_stringify_keys
|
|
@@ -317,8 +372,8 @@ Stringifies keys of nested hash without mutating itself. Does not handle nested
|
|
|
317
372
|
[source,ruby]
|
|
318
373
|
----
|
|
319
374
|
example = {a: {b: 2}}
|
|
320
|
-
example.deep_stringify_keys #
|
|
321
|
-
example #
|
|
375
|
+
example.deep_stringify_keys # {"a" => {"b" => 1}}
|
|
376
|
+
example # {a: {b: 2}}
|
|
322
377
|
----
|
|
323
378
|
|
|
324
379
|
===== #deep_stringify_keys!
|
|
@@ -328,8 +383,8 @@ Stringifies keys of nested hash while mutating itself. Does not handle nested ar
|
|
|
328
383
|
[source,ruby]
|
|
329
384
|
----
|
|
330
385
|
example = {a: {b: 2}}
|
|
331
|
-
example.deep_stringify_keys! #
|
|
332
|
-
example #
|
|
386
|
+
example.deep_stringify_keys! # {"a" => {"b" => 1}}
|
|
387
|
+
example # {"a" => {"b" => 1}}
|
|
333
388
|
----
|
|
334
389
|
|
|
335
390
|
===== #deep_symbolize_keys
|
|
@@ -339,8 +394,8 @@ Symbolizes keys of nested hash without mutating itself. Does not handle nested a
|
|
|
339
394
|
[source,ruby]
|
|
340
395
|
----
|
|
341
396
|
example = {"a" => {"b" => 2}}
|
|
342
|
-
example.deep_symbolize_keys #
|
|
343
|
-
example #
|
|
397
|
+
example.deep_symbolize_keys # {a: {b: 1}}
|
|
398
|
+
example # {"a" => {"b" => 2}}
|
|
344
399
|
----
|
|
345
400
|
|
|
346
401
|
===== #deep_symbolize_keys!
|
|
@@ -350,8 +405,24 @@ Symbolizes keys of nested hash while mutating itself. Does not handle nested arr
|
|
|
350
405
|
[source,ruby]
|
|
351
406
|
----
|
|
352
407
|
example = {"a" => {"b" => 2}}
|
|
353
|
-
example.deep_symbolize_keys! #
|
|
354
|
-
example #
|
|
408
|
+
example.deep_symbolize_keys! # {a: {b: 1}}
|
|
409
|
+
example # {a: {b: 1}}
|
|
410
|
+
----
|
|
411
|
+
|
|
412
|
+
===== #fetch_value
|
|
413
|
+
|
|
414
|
+
Fetches value for exiting or missing key. Behavior is identical to `#fetch` except when the value of
|
|
415
|
+
the key is `nil` you'll get the default value instead. This eliminates the need for using an _or_
|
|
416
|
+
expression `example.fetch(:desired_key) || "default_value"`.
|
|
417
|
+
|
|
418
|
+
[source,ruby]
|
|
419
|
+
----
|
|
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
|
|
355
426
|
----
|
|
356
427
|
|
|
357
428
|
===== #flatten_keys
|
|
@@ -361,15 +432,15 @@ though.
|
|
|
361
432
|
|
|
362
433
|
[source,ruby]
|
|
363
434
|
----
|
|
364
|
-
{a: {b: 1}}.flatten_keys prefix: :test
|
|
365
|
-
{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}
|
|
366
437
|
|
|
367
|
-
{a: {b: 1}}.flatten_keys cast: :to_s #
|
|
368
|
-
{"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}
|
|
369
440
|
|
|
370
441
|
example = {a: {b: 1}}
|
|
371
|
-
example.flatten_keys
|
|
372
|
-
example
|
|
442
|
+
example.flatten_keys # {a_b: 1}
|
|
443
|
+
example # {a: {b: 1}}
|
|
373
444
|
----
|
|
374
445
|
|
|
375
446
|
===== #flatten_keys!
|
|
@@ -380,8 +451,8 @@ though.
|
|
|
380
451
|
[source,ruby]
|
|
381
452
|
----
|
|
382
453
|
example = {a: {b: 1}}
|
|
383
|
-
example.flatten_keys! #
|
|
384
|
-
example #
|
|
454
|
+
example.flatten_keys! # {a_b: 1}
|
|
455
|
+
example # {a_b: 1}
|
|
385
456
|
----
|
|
386
457
|
|
|
387
458
|
===== #recurse
|
|
@@ -392,8 +463,8 @@ handle nested arrays, though.
|
|
|
392
463
|
[source,ruby]
|
|
393
464
|
----
|
|
394
465
|
example = {"a" => {"b" => 1}}
|
|
395
|
-
example.recurse(&:symbolize_keys) #
|
|
396
|
-
example.recurse(&:invert) #
|
|
466
|
+
example.recurse(&:symbolize_keys) # {a: {b: 1}}
|
|
467
|
+
example.recurse(&:invert) # {{"b" => 1} => "a"}
|
|
397
468
|
----
|
|
398
469
|
|
|
399
470
|
===== #stringify_keys
|
|
@@ -403,8 +474,8 @@ Converts keys to strings without mutating itself.
|
|
|
403
474
|
[source,ruby]
|
|
404
475
|
----
|
|
405
476
|
example = {a: 1, b: 2}
|
|
406
|
-
example.stringify_keys #
|
|
407
|
-
example #
|
|
477
|
+
example.stringify_keys # {"a" => 1, "b" => 2}
|
|
478
|
+
example # {a: 1, b: 2}
|
|
408
479
|
----
|
|
409
480
|
|
|
410
481
|
===== #stringify_keys!
|
|
@@ -414,8 +485,8 @@ Converts keys to strings while mutating itself.
|
|
|
414
485
|
[source,ruby]
|
|
415
486
|
----
|
|
416
487
|
example = {a: 1, b: 2}
|
|
417
|
-
example.stringify_keys! #
|
|
418
|
-
example #
|
|
488
|
+
example.stringify_keys! # {"a" => 1, "b" => 2}
|
|
489
|
+
example # {"a" => 1, "b" => 2}
|
|
419
490
|
----
|
|
420
491
|
|
|
421
492
|
===== #symbolize_keys
|
|
@@ -425,8 +496,8 @@ Converts keys to symbols without mutating itself.
|
|
|
425
496
|
[source,ruby]
|
|
426
497
|
----
|
|
427
498
|
example = {"a" => 1, "b" => 2}
|
|
428
|
-
example.symbolize_keys #
|
|
429
|
-
example #
|
|
499
|
+
example.symbolize_keys # {a: 1, b: 2}
|
|
500
|
+
example # {"a" => 1, "b" => 2}
|
|
430
501
|
----
|
|
431
502
|
|
|
432
503
|
===== #symbolize_keys!
|
|
@@ -436,8 +507,8 @@ Converts keys to symbols while mutating itself.
|
|
|
436
507
|
[source,ruby]
|
|
437
508
|
----
|
|
438
509
|
example = {"a" => 1, "b" => 2}
|
|
439
|
-
example.symbolize_keys! #
|
|
440
|
-
example #
|
|
510
|
+
example.symbolize_keys! # {a: 1, b: 2}
|
|
511
|
+
example # {a: 1, b: 2}
|
|
441
512
|
----
|
|
442
513
|
|
|
443
514
|
===== #use
|
|
@@ -447,7 +518,8 @@ Passes each hash value as a block argument for further processing.
|
|
|
447
518
|
[source,ruby]
|
|
448
519
|
----
|
|
449
520
|
example = {unit: "221B", street: "Baker Street", city: "London", country: "UK"}
|
|
450
|
-
|
|
521
|
+
|
|
522
|
+
example.use { |unit, street| "#{unit} #{street}" } # "221B Baker Street"
|
|
451
523
|
----
|
|
452
524
|
|
|
453
525
|
==== IO
|
|
@@ -460,8 +532,8 @@ block, you'll need to close the stream manually.
|
|
|
460
532
|
|
|
461
533
|
[source,ruby]
|
|
462
534
|
----
|
|
463
|
-
io = IO.void #
|
|
464
|
-
io = IO.void { |void| void.write "nevermore" } #
|
|
535
|
+
io = IO.void # "#<IO:fd 20>"
|
|
536
|
+
io = IO.void { |void| void.write "nevermore" } # "#<IO:(closed)>"
|
|
465
537
|
----
|
|
466
538
|
|
|
467
539
|
===== #redirect
|
|
@@ -474,8 +546,8 @@ answered instead.
|
|
|
474
546
|
io = IO.new IO.sysopen(Pathname("test.txt").to_s, "w+")
|
|
475
547
|
other = IO.new IO.sysopen(Pathname("other.txt").to_s, "w+")
|
|
476
548
|
|
|
477
|
-
io.redirect other #
|
|
478
|
-
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>"
|
|
479
551
|
----
|
|
480
552
|
|
|
481
553
|
===== #reread
|
|
@@ -487,12 +559,12 @@ Answers full stream by rewinding to beginning of stream and reading all content.
|
|
|
487
559
|
io = IO.new IO.sysopen(Pathname("test.txt").to_s, "w+")
|
|
488
560
|
io.write "This is a test."
|
|
489
561
|
|
|
490
|
-
io.reread
|
|
491
|
-
io.reread 4
|
|
562
|
+
io.reread # "This is a test."
|
|
563
|
+
io.reread 4 # "This"
|
|
492
564
|
|
|
493
565
|
buffer = "".dup
|
|
494
|
-
io.reread(buffer: buffer) #
|
|
495
|
-
buffer #
|
|
566
|
+
io.reread(buffer: buffer) # "This is a test."
|
|
567
|
+
buffer # "This is a test."
|
|
496
568
|
----
|
|
497
569
|
|
|
498
570
|
===== #squelch
|
|
@@ -503,9 +575,10 @@ arguments or when given a block.
|
|
|
503
575
|
[source,ruby]
|
|
504
576
|
----
|
|
505
577
|
io = IO.new IO.sysopen(Pathname("test.txt").to_s, "w+")
|
|
506
|
-
|
|
507
|
-
io.squelch
|
|
508
|
-
io.
|
|
578
|
+
|
|
579
|
+
io.squelch # "#<IO:fd 20>"
|
|
580
|
+
io.squelch { io.write "Test" } # "#<IO:fd 20>"
|
|
581
|
+
io.reread # ""
|
|
509
582
|
----
|
|
510
583
|
|
|
511
584
|
==== Pathname
|
|
@@ -519,7 +592,7 @@ construct a valid path.
|
|
|
519
592
|
|
|
520
593
|
[source,ruby]
|
|
521
594
|
----
|
|
522
|
-
Pathname(nil)
|
|
595
|
+
Pathname(nil) # Pathname("")
|
|
523
596
|
----
|
|
524
597
|
|
|
525
598
|
===== .home
|
|
@@ -528,7 +601,7 @@ Answers user home directory.
|
|
|
528
601
|
|
|
529
602
|
[source,ruby]
|
|
530
603
|
----
|
|
531
|
-
Pathname.home #
|
|
604
|
+
Pathname.home # Pathname "/Users/demo"
|
|
532
605
|
----
|
|
533
606
|
|
|
534
607
|
===== .make_temp_dir
|
|
@@ -545,13 +618,13 @@ reality, these paths will be longer depending on which operating system you are
|
|
|
545
618
|
|
|
546
619
|
[source,ruby]
|
|
547
620
|
----
|
|
548
|
-
Pathname.make_temp_dir #
|
|
549
|
-
Pathname.make_temp_dir prefix: "prefix-" #
|
|
550
|
-
Pathname.make_temp_dir suffix: "-suffix" #
|
|
551
|
-
Pathname.make_temp_dir prefix: "prefix-", suffix: "-suffix" #
|
|
552
|
-
Pathname.make_temp_dir root: "/example" #
|
|
553
|
-
Pathname.make_temp_dir { "I am a block result" } #
|
|
554
|
-
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
|
|
555
628
|
----
|
|
556
629
|
|
|
557
630
|
===== .require_tree
|
|
@@ -588,7 +661,7 @@ Answers operating system root path.
|
|
|
588
661
|
|
|
589
662
|
[source,ruby]
|
|
590
663
|
----
|
|
591
|
-
Pathname.root #
|
|
664
|
+
Pathname.root # Pathname "/"
|
|
592
665
|
----
|
|
593
666
|
|
|
594
667
|
===== #change_dir
|
|
@@ -598,13 +671,13 @@ link:https://rubyapi.org/o/Dir.chdir#method-c-chdir[Dir.chdir] for details.
|
|
|
598
671
|
|
|
599
672
|
[source,ruby]
|
|
600
673
|
----
|
|
601
|
-
Pathname.pwd
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
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)
|
|
608
681
|
----
|
|
609
682
|
|
|
610
683
|
===== #copy
|
|
@@ -613,7 +686,31 @@ Copies file from current location to new location while answering itself so it c
|
|
|
613
686
|
|
|
614
687
|
[source,ruby]
|
|
615
688
|
----
|
|
616
|
-
Pathname("input.txt").copy Pathname("output.txt") #
|
|
689
|
+
Pathname("input.txt").copy Pathname("output.txt") # Pathname("input.txt")
|
|
690
|
+
----
|
|
691
|
+
|
|
692
|
+
===== #deep_touch
|
|
693
|
+
|
|
694
|
+
Has all of the same functionality as the `#touch` method while being able to create ancestor
|
|
695
|
+
directories no matter how deeply nested the file might be.
|
|
696
|
+
|
|
697
|
+
[source,ruby]
|
|
698
|
+
----
|
|
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")
|
|
701
|
+
----
|
|
702
|
+
|
|
703
|
+
===== #delete
|
|
704
|
+
|
|
705
|
+
Deletes file or directory and answers itself so it can be chained.
|
|
706
|
+
|
|
707
|
+
[source,ruby]
|
|
708
|
+
----
|
|
709
|
+
# When path exists.
|
|
710
|
+
Pathname("/example.txt").touch.delete # Pathname("/example")
|
|
711
|
+
|
|
712
|
+
# When path doesn't exist.
|
|
713
|
+
Pathname("/example.txt").delete # Errno::ENOENT
|
|
617
714
|
----
|
|
618
715
|
|
|
619
716
|
===== #directories
|
|
@@ -622,9 +719,23 @@ Answers all directories or filtered directories for current path.
|
|
|
622
719
|
|
|
623
720
|
[source,ruby]
|
|
624
721
|
----
|
|
625
|
-
Pathname("/example").directories #
|
|
626
|
-
Pathname("/example").directories "a*" #
|
|
627
|
-
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(".")]
|
|
725
|
+
----
|
|
726
|
+
|
|
727
|
+
===== #empty
|
|
728
|
+
|
|
729
|
+
Empties a directory of children (i.e. folders, nested folders, or files) or clears an existing file
|
|
730
|
+
of contents. If a directory or file doesn't exist, it will be created.
|
|
731
|
+
|
|
732
|
+
[source,ruby]
|
|
733
|
+
----
|
|
734
|
+
directory = Pathname("test").make_path
|
|
735
|
+
file = directory.join("test.txt").write("example")
|
|
736
|
+
|
|
737
|
+
file.empty.read # ""
|
|
738
|
+
directory.empty.children # []
|
|
628
739
|
----
|
|
629
740
|
|
|
630
741
|
===== #extensions
|
|
@@ -633,7 +744,7 @@ Answers file extensions as an array.
|
|
|
633
744
|
|
|
634
745
|
[source,ruby]
|
|
635
746
|
----
|
|
636
|
-
Pathname("example.txt.erb").extensions #
|
|
747
|
+
Pathname("example.txt.erb").extensions # [".txt", ".erb"]
|
|
637
748
|
----
|
|
638
749
|
|
|
639
750
|
===== #files
|
|
@@ -642,9 +753,9 @@ Answers all files or filtered files for current path.
|
|
|
642
753
|
|
|
643
754
|
[source,ruby]
|
|
644
755
|
----
|
|
645
|
-
Pathname("/example").files #
|
|
646
|
-
Pathname("/example").files "*.png" #
|
|
647
|
-
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")]
|
|
648
759
|
----
|
|
649
760
|
|
|
650
761
|
===== #gsub
|
|
@@ -654,10 +765,10 @@ Same behavior as `String#gsub` but answers a path with patterns replaced with de
|
|
|
654
765
|
[source,ruby]
|
|
655
766
|
----
|
|
656
767
|
Pathname("/a/path/some/path").gsub("path", "test")
|
|
657
|
-
#
|
|
768
|
+
# Pathname("/a/test/some/test")
|
|
658
769
|
|
|
659
770
|
Pathname("/%placeholder%/some/%placeholder%").gsub("%placeholder%", "test")
|
|
660
|
-
#
|
|
771
|
+
# Pathname("/test/some/test")
|
|
661
772
|
----
|
|
662
773
|
|
|
663
774
|
===== #make_ancestors
|
|
@@ -666,9 +777,9 @@ Ensures all ancestor directories are created for a path.
|
|
|
666
777
|
|
|
667
778
|
[source,ruby]
|
|
668
779
|
----
|
|
669
|
-
Pathname("/one/two").make_ancestors #
|
|
670
|
-
Pathname("/one").exist? #
|
|
671
|
-
Pathname("/one/two").exist? #
|
|
780
|
+
Pathname("/one/two").make_ancestors # Pathname("/one/two")
|
|
781
|
+
Pathname("/one").exist? # true
|
|
782
|
+
Pathname("/one/two").exist? # false
|
|
672
783
|
----
|
|
673
784
|
|
|
674
785
|
===== #make_dir
|
|
@@ -678,8 +789,8 @@ not throwing errors when directory does exist in order to ensure the pathname ca
|
|
|
678
789
|
|
|
679
790
|
[source,ruby]
|
|
680
791
|
----
|
|
681
|
-
Pathname("/one").make_dir #
|
|
682
|
-
Pathname("/one").make_dir.make_dir #
|
|
792
|
+
Pathname("/one").make_dir # Pathname("/one")
|
|
793
|
+
Pathname("/one").make_dir.make_dir # Pathname("/one")
|
|
683
794
|
----
|
|
684
795
|
|
|
685
796
|
===== #make_path
|
|
@@ -689,8 +800,8 @@ not throwing errors when directory does exist in order to ensure the pathname ca
|
|
|
689
800
|
|
|
690
801
|
[source,ruby]
|
|
691
802
|
----
|
|
692
|
-
Pathname("/one/two/three").make_path #
|
|
693
|
-
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")
|
|
694
805
|
----
|
|
695
806
|
|
|
696
807
|
===== #name
|
|
@@ -699,7 +810,7 @@ Answers file name without extension.
|
|
|
699
810
|
|
|
700
811
|
[source,ruby]
|
|
701
812
|
----
|
|
702
|
-
Pathname("example.txt").name #
|
|
813
|
+
Pathname("example.txt").name # Pathname("example")
|
|
703
814
|
----
|
|
704
815
|
|
|
705
816
|
===== #relative_parent
|
|
@@ -708,7 +819,7 @@ Answers relative path from parent directory. This is a complement to `#relative_
|
|
|
708
819
|
|
|
709
820
|
[source,ruby]
|
|
710
821
|
----
|
|
711
|
-
Pathname("/one/two/three").relative_parent("/one")
|
|
822
|
+
Pathname("/one/two/three").relative_parent("/one") # Pathname "two"
|
|
712
823
|
----
|
|
713
824
|
|
|
714
825
|
===== #remove_dir
|
|
@@ -718,9 +829,9 @@ not throwing errors when directory does exist in order to ensure the pathname ca
|
|
|
718
829
|
|
|
719
830
|
[source,ruby]
|
|
720
831
|
----
|
|
721
|
-
Pathname("/test").make_dir.remove_dir.exist? #
|
|
722
|
-
Pathname("/test").remove_dir #
|
|
723
|
-
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")
|
|
724
835
|
----
|
|
725
836
|
|
|
726
837
|
===== #remove_tree
|
|
@@ -734,14 +845,14 @@ parent_path = Pathname "/one"
|
|
|
734
845
|
child_path = parent_path.join "two"
|
|
735
846
|
|
|
736
847
|
child_path.make_path
|
|
737
|
-
child_path.remove_tree
|
|
738
|
-
child_path.exist?
|
|
739
|
-
paremt_path.exist?
|
|
848
|
+
child_path.remove_tree # Pathname "/one/two"
|
|
849
|
+
child_path.exist? # false
|
|
850
|
+
paremt_path.exist? # true
|
|
740
851
|
|
|
741
852
|
child_path.make_path
|
|
742
|
-
parent_path.remove_tree #
|
|
743
|
-
child_path.exist? #
|
|
744
|
-
parent_path.exist? #
|
|
853
|
+
parent_path.remove_tree # Pathname "/one"
|
|
854
|
+
child_path.exist? # false
|
|
855
|
+
parent_path.exist? # false
|
|
745
856
|
----
|
|
746
857
|
|
|
747
858
|
===== #rewrite
|
|
@@ -751,18 +862,21 @@ immediate writing back to the same file.
|
|
|
751
862
|
|
|
752
863
|
[source,ruby]
|
|
753
864
|
----
|
|
754
|
-
Pathname("/test.txt").rewrite #
|
|
755
|
-
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")
|
|
756
867
|
----
|
|
757
868
|
|
|
758
869
|
===== #touch
|
|
759
870
|
|
|
760
|
-
Updates access and modification times for path
|
|
871
|
+
Updates access and modification times for an existing path by defaulting to current time. When path
|
|
872
|
+
doesn't exist, it will be created as a file.
|
|
761
873
|
|
|
762
874
|
[source,ruby]
|
|
763
875
|
----
|
|
764
|
-
Pathname("example
|
|
765
|
-
Pathname("example
|
|
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")
|
|
766
880
|
----
|
|
767
881
|
|
|
768
882
|
===== #write
|
|
@@ -772,9 +886,9 @@ options.
|
|
|
772
886
|
|
|
773
887
|
[source,ruby]
|
|
774
888
|
----
|
|
775
|
-
Pathname("example.txt").write "test" #
|
|
776
|
-
Pathname("example.txt").write "test", offset: 1 #
|
|
777
|
-
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")
|
|
778
892
|
----
|
|
779
893
|
|
|
780
894
|
==== String
|
|
@@ -785,7 +899,7 @@ Answers `true`/`false` based on whether string is blank, `<space>`, `\n`, `\t`,
|
|
|
785
899
|
|
|
786
900
|
[source,ruby]
|
|
787
901
|
----
|
|
788
|
-
" \n\t\r".blank?
|
|
902
|
+
" \n\t\r".blank? # true
|
|
789
903
|
----
|
|
790
904
|
|
|
791
905
|
===== #camelcase
|
|
@@ -794,7 +908,7 @@ Answers a camelcased string.
|
|
|
794
908
|
|
|
795
909
|
[source,ruby]
|
|
796
910
|
----
|
|
797
|
-
"this_is_an_example".camelcase
|
|
911
|
+
"this_is_an_example".camelcase # "ThisIsAnExample"
|
|
798
912
|
----
|
|
799
913
|
|
|
800
914
|
===== #down
|
|
@@ -803,7 +917,7 @@ Answers string with only first letter downcased.
|
|
|
803
917
|
|
|
804
918
|
[source,ruby]
|
|
805
919
|
----
|
|
806
|
-
"EXAMPLE".down
|
|
920
|
+
"EXAMPLE".down # "eXAMPLE"
|
|
807
921
|
----
|
|
808
922
|
|
|
809
923
|
===== #first
|
|
@@ -812,8 +926,8 @@ Answers first character of a string or first set of characters if given a number
|
|
|
812
926
|
|
|
813
927
|
[source,ruby]
|
|
814
928
|
----
|
|
815
|
-
"example".first #
|
|
816
|
-
"example".first 4 #
|
|
929
|
+
"example".first # "e"
|
|
930
|
+
"example".first 4 # "exam"
|
|
817
931
|
----
|
|
818
932
|
|
|
819
933
|
===== #indent
|
|
@@ -822,11 +936,11 @@ Answers string indented by two spaces by default.
|
|
|
822
936
|
|
|
823
937
|
[source,ruby]
|
|
824
938
|
----
|
|
825
|
-
"example".indent #
|
|
826
|
-
"example".indent 0 #
|
|
827
|
-
"example".indent -1 #
|
|
828
|
-
"example".indent 2 #
|
|
829
|
-
"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"
|
|
830
944
|
----
|
|
831
945
|
|
|
832
946
|
===== #last
|
|
@@ -835,8 +949,48 @@ Answers last character of a string or last set of characters if given a number.
|
|
|
835
949
|
|
|
836
950
|
[source,ruby]
|
|
837
951
|
----
|
|
838
|
-
"instant".last #
|
|
839
|
-
"instant".last 3 #
|
|
952
|
+
"instant".last # "t"
|
|
953
|
+
"instant".last 3 # "ant"
|
|
954
|
+
----
|
|
955
|
+
|
|
956
|
+
===== #pluralize
|
|
957
|
+
|
|
958
|
+
Answers plural form of self when given a suffix to add. The plural form of the word can be
|
|
959
|
+
dynamically calculated when given a count and a replacement pattern (i.e. string or regular
|
|
960
|
+
expression) can be supplied for further specificity. Usage is based on
|
|
961
|
+
link:https://en.wikipedia.org/wiki/English_plurals[plurals in English] which may or may not work
|
|
962
|
+
well in other languages.
|
|
963
|
+
|
|
964
|
+
[source,ruby]
|
|
965
|
+
----
|
|
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
|
|
974
|
+
----
|
|
975
|
+
|
|
976
|
+
===== #singularize
|
|
977
|
+
|
|
978
|
+
Answers singular form of self when given a suffix to remove (can be a string or a regular
|
|
979
|
+
expression). The singular form of the word can be dynamically calculated when given a count and a
|
|
980
|
+
replacement string can be supplied for further specificity. Usage is based on
|
|
981
|
+
link:https://en.wikipedia.org/wiki/English_plurals[plurals in English] which may or may not work
|
|
982
|
+
well in other languages.
|
|
983
|
+
|
|
984
|
+
[source,ruby]
|
|
985
|
+
----
|
|
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
|
|
840
994
|
----
|
|
841
995
|
|
|
842
996
|
===== #snakecase
|
|
@@ -845,7 +999,7 @@ Answers a snakecased string.
|
|
|
845
999
|
|
|
846
1000
|
[source,ruby]
|
|
847
1001
|
----
|
|
848
|
-
"ThisIsAnExample".snakecase
|
|
1002
|
+
"ThisIsAnExample".snakecase # "this_is_an_example"
|
|
849
1003
|
----
|
|
850
1004
|
|
|
851
1005
|
===== #titleize
|
|
@@ -854,7 +1008,7 @@ Answers titleized string.
|
|
|
854
1008
|
|
|
855
1009
|
[source,ruby]
|
|
856
1010
|
----
|
|
857
|
-
"ThisIsAnExample".titleize
|
|
1011
|
+
"ThisIsAnExample".titleize # "This Is An Example"
|
|
858
1012
|
----
|
|
859
1013
|
|
|
860
1014
|
===== #to_bool
|
|
@@ -863,11 +1017,11 @@ Answers string as a boolean.
|
|
|
863
1017
|
|
|
864
1018
|
[source,ruby]
|
|
865
1019
|
----
|
|
866
|
-
"true".to_bool #
|
|
867
|
-
"yes".to_bool #
|
|
868
|
-
"1".to_bool #
|
|
869
|
-
"".to_bool #
|
|
870
|
-
"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
|
|
871
1025
|
----
|
|
872
1026
|
|
|
873
1027
|
===== #up
|
|
@@ -876,7 +1030,7 @@ Answers string with only first letter upcased.
|
|
|
876
1030
|
|
|
877
1031
|
[source,ruby]
|
|
878
1032
|
----
|
|
879
|
-
"example".up
|
|
1033
|
+
"example".up # "Example"
|
|
880
1034
|
----
|
|
881
1035
|
|
|
882
1036
|
==== String IO
|
|
@@ -890,12 +1044,12 @@ Answers full string by rewinding to beginning of string and reading all content.
|
|
|
890
1044
|
io = StringIO.new
|
|
891
1045
|
io.write "This is a test."
|
|
892
1046
|
|
|
893
|
-
io.reread #
|
|
894
|
-
io.reread 4 #
|
|
1047
|
+
io.reread # "This is a test."
|
|
1048
|
+
io.reread 4 # "This"
|
|
895
1049
|
|
|
896
1050
|
buffer = "".dup
|
|
897
|
-
io.reread(buffer: buffer) #
|
|
898
|
-
buffer #
|
|
1051
|
+
io.reread(buffer: buffer) # "This is a test."
|
|
1052
|
+
buffer # "This is a test."
|
|
899
1053
|
----
|
|
900
1054
|
|
|
901
1055
|
==== Struct
|
|
@@ -906,8 +1060,8 @@ Answers whether a struct was constructed with keyword or positional arguments.
|
|
|
906
1060
|
|
|
907
1061
|
[source,ruby]
|
|
908
1062
|
----
|
|
909
|
-
Struct.new(:a, keyword_init: true).keyworded? #
|
|
910
|
-
Struct.new(:a).keyworded? #
|
|
1063
|
+
Struct.new(:a, keyword_init: true).keyworded? # true
|
|
1064
|
+
Struct.new(:a).keyworded? # false
|
|
911
1065
|
----
|
|
912
1066
|
|
|
913
1067
|
===== .with_keywords
|
|
@@ -918,14 +1072,14 @@ whether the struct was constructed with positional or keyword arguments.
|
|
|
918
1072
|
[source,ruby]
|
|
919
1073
|
----
|
|
920
1074
|
Example = Struct.new :a, :b, :c
|
|
921
|
-
Example.with_keywords a: 1, b: 2, c: 3 #
|
|
922
|
-
Example.with_keywords a: 1 #
|
|
923
|
-
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>"
|
|
924
1078
|
|
|
925
1079
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
|
926
|
-
Example.with_keywords a: 1, b: 2, c: 3 #
|
|
927
|
-
Example.with_keywords a: 1 #
|
|
928
|
-
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>"
|
|
929
1083
|
----
|
|
930
1084
|
|
|
931
1085
|
===== .with_positions
|
|
@@ -936,12 +1090,12 @@ whether the struct was constructed with positional or keyword arguments.
|
|
|
936
1090
|
[source,ruby]
|
|
937
1091
|
----
|
|
938
1092
|
Example = Struct.new :a, :b, :c
|
|
939
|
-
Example.with_positions 1, 2, 3 #
|
|
940
|
-
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>"
|
|
941
1095
|
|
|
942
1096
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
|
943
|
-
Example.with_positions 1, 2, 3 #
|
|
944
|
-
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>"
|
|
945
1099
|
----
|
|
946
1100
|
|
|
947
1101
|
===== #merge
|
|
@@ -952,17 +1106,17 @@ Merges multiple attributes without mutating itself.
|
|
|
952
1106
|
----
|
|
953
1107
|
Example = Struct.new :a, :b, :c
|
|
954
1108
|
example = Example[1, 2, 3]
|
|
955
|
-
example.merge a: 10 #
|
|
956
|
-
example.merge a: 10, c: 30 #
|
|
957
|
-
example.merge a: 10, b: 20, c: 30 #
|
|
958
|
-
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>"
|
|
959
1113
|
|
|
960
1114
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
|
961
1115
|
example = Example[a: 1, b: 2, c: 3]
|
|
962
|
-
example.merge a: 10 #
|
|
963
|
-
example.merge a: 10, c: 30 #
|
|
964
|
-
example.merge a: 10, b: 20, c: 30 #
|
|
965
|
-
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>"
|
|
966
1120
|
----
|
|
967
1121
|
|
|
968
1122
|
===== #merge!
|
|
@@ -973,17 +1127,17 @@ Merges multiple attributes while mutating itself.
|
|
|
973
1127
|
----
|
|
974
1128
|
Example = Struct.new :a, :b, :c
|
|
975
1129
|
example = Example[1, 2, 3]
|
|
976
|
-
example.merge! a: 10 #
|
|
977
|
-
example.merge! a: 10, c: 30 #
|
|
978
|
-
example.merge! a: 10, b: 20, c: 30 #
|
|
979
|
-
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>"
|
|
980
1134
|
|
|
981
1135
|
Example = Struct.new :a, :b, :c, keyword_init: true
|
|
982
1136
|
example = Example[a: 1, b: 2, c: 3]
|
|
983
|
-
example.merge! a: 10 #
|
|
984
|
-
example.merge! a: 10, c: 30 #
|
|
985
|
-
example.merge! a: 10, b: 20, c: 30 #
|
|
986
|
-
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>"
|
|
987
1141
|
----
|
|
988
1142
|
|
|
989
1143
|
===== #revalue
|
|
@@ -999,11 +1153,11 @@ below but a keyword struct would work too.
|
|
|
999
1153
|
Example = Struct.new :a, :b, :c
|
|
1000
1154
|
|
|
1001
1155
|
example = Example[1, 2, 3]
|
|
1002
|
-
example.revalue { |value| value * 2 } #
|
|
1003
|
-
example.revalue(c: 2) { |previous, current| previous + current } #
|
|
1004
|
-
example.revalue c: 2 #
|
|
1005
|
-
example.revalue #
|
|
1006
|
-
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>"
|
|
1007
1161
|
|
|
1008
1162
|
----
|
|
1009
1163
|
|
|
@@ -1020,19 +1174,37 @@ keyword struct would work too.
|
|
|
1020
1174
|
Example = Struct.new :a, :b, :c
|
|
1021
1175
|
|
|
1022
1176
|
example = Example[1, 2, 3]
|
|
1023
|
-
example.revalue! { |value| value * 2 } #
|
|
1024
|
-
example #
|
|
1177
|
+
example.revalue! { |value| value * 2 } # "#<struct a=2, b=4, c=6>"
|
|
1178
|
+
example # "#<struct a=2, b=4, c=6>"
|
|
1025
1179
|
|
|
1026
1180
|
example = Example[1, 2, 3]
|
|
1027
|
-
example.revalue!(c: 2) { |previous, current| previous + current } #
|
|
1028
|
-
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>"
|
|
1029
1183
|
|
|
1030
1184
|
example = Example[1, 2, 3]
|
|
1031
|
-
example.revalue! c: 2 #
|
|
1032
|
-
example.revalue! #
|
|
1033
|
-
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>"
|
|
1034
1188
|
----
|
|
1035
1189
|
|
|
1190
|
+
==== Symbol
|
|
1191
|
+
|
|
1192
|
+
===== #call
|
|
1193
|
+
|
|
1194
|
+
Enhances symbol-to-proc by allowing you to send additional arguments and/or a block. This only works
|
|
1195
|
+
with public methods in order to not break encapsulation.
|
|
1196
|
+
|
|
1197
|
+
[source,ruby]
|
|
1198
|
+
----
|
|
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"]
|
|
1203
|
+
----
|
|
1204
|
+
|
|
1205
|
+
⚠️ Use of `#call` without any arguments or block should be avoided in order to not incur extra
|
|
1206
|
+
processing costs since the original symbol-to-proc call can used instead.
|
|
1207
|
+
|
|
1036
1208
|
== Development
|
|
1037
1209
|
|
|
1038
1210
|
To contribute, run:
|