fat_core 7.1.3 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.yardopts +3 -4
- data/Gemfile +1 -0
- data/README.md +1199 -0
- data/README.org +266 -225
- data/Rakefile +3 -7
- data/lib/fat_core/all.rb +1 -0
- data/lib/fat_core/string.rb +34 -13
- data/lib/fat_core/version.rb +2 -2
- data/lib/fat_core.rb +12 -2
- data/spec/lib/string_spec.rb +9 -0
- metadata +3 -2
data/README.org
CHANGED
|
@@ -1,96 +1,27 @@
|
|
|
1
1
|
#+TITLE: FatCore Guide
|
|
2
2
|
#+OPTIONS: toc:5
|
|
3
|
-
#+PROPERTY: header-args:ruby :colnames no :hlines yes :exports both :
|
|
4
|
-
#+PROPERTY: header-args:
|
|
3
|
+
#+PROPERTY: header-args:ruby :results value :colnames no :hlines yes :exports both :dir "./"
|
|
4
|
+
#+PROPERTY: header-args:ruby+ :wrap example :eval yes
|
|
5
|
+
#+PROPERTY: header-args:ruby+ :prologue "$:.unshift('./lib') unless $:.first == './lib'; require 'fat_core/all'; nil"
|
|
6
|
+
#+PROPERTY: header-args:ruby+ :session fat_core_session :ruby "bundle exec irb"
|
|
7
|
+
#+PROPERTY: header-args:sh :exports code :eval no
|
|
8
|
+
#+PROPERTY: header-args:bash :exports code :eval no
|
|
5
9
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Run this block before all others to ensure that we are reading the libraries
|
|
10
|
-
from the source directory.
|
|
11
|
-
|
|
12
|
-
#+begin_src ruby :results output
|
|
13
|
-
puts "Current directory: #{Dir.pwd}"
|
|
14
|
-
puts "Ruby LOADPATH:"
|
|
15
|
-
$:.unshift("./lib") unless $:[0] == './lib'
|
|
16
|
-
$:[0..10].each { |d| puts d }
|
|
17
|
-
puts "..."
|
|
18
|
-
require_relative 'lib/fat_core/all' # => true
|
|
19
|
-
#+end_src
|
|
20
|
-
|
|
21
|
-
#+RESULTS:
|
|
22
|
-
#+begin_example
|
|
23
|
-
Current directory: /home/ded/src/fat_core
|
|
24
|
-
Ruby LOADPATH:
|
|
25
|
-
./lib
|
|
26
|
-
/home/ded/.rbenv/rbenv.d/exec/gem-rehash
|
|
27
|
-
/home/ded/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0
|
|
28
|
-
/home/ded/.rbenv/versions/3.2.2/lib/ruby/site_ruby/3.2.0/x86_64-linux
|
|
29
|
-
/home/ded/.rbenv/versions/3.2.2/lib/ruby/site_ruby
|
|
30
|
-
/home/ded/.rbenv/versions/3.2.2/lib/ruby/vendor_ruby/3.2.0
|
|
31
|
-
/home/ded/.rbenv/versions/3.2.2/lib/ruby/vendor_ruby/3.2.0/x86_64-linux
|
|
32
|
-
/home/ded/.rbenv/versions/3.2.2/lib/ruby/vendor_ruby
|
|
33
|
-
/home/ded/.rbenv/versions/3.2.2/lib/ruby/3.2.0
|
|
34
|
-
/home/ded/.rbenv/versions/3.2.2/lib/ruby/3.2.0/x86_64-linux
|
|
35
|
-
...
|
|
36
|
-
#+end_example
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* Table of Contents :toc:noexport:
|
|
40
|
-
- [[#version][Version]]
|
|
41
|
-
- [[#fatcore][FatCore]]
|
|
42
|
-
- [[#installation][Installation]]
|
|
43
|
-
- [[#usage][Usage]]
|
|
44
|
-
- [[#array][Array]]
|
|
45
|
-
- [[#method-comma_joinsep-nil-last_sep-nil-two_sep-nil][Method ~#comma_join(sep: nil, last_sep: nil, two_sep: nil)~]]
|
|
46
|
-
- [[#method-last_i][Method ~#last_i~]]
|
|
47
|
-
- [[#method-intersect_with_dups][Method ~#intersect_with_dups~]]
|
|
48
|
-
- [[#method-diff_with_dups][Method ~diff_with_dups~]]
|
|
49
|
-
- [[#bigdecimal-inspect][BigDecimal ~#inspect~]]
|
|
50
|
-
- [[#enumerable][Enumerable]]
|
|
51
|
-
- [[#method-each_with_flags][Method ~#each_with_flags~]]
|
|
52
|
-
- [[#hash][Hash]]
|
|
53
|
-
- [[#method-each_pair_with_flags][Method ~#each_pair_with_flags~]]
|
|
54
|
-
- [[#method-delete_with_value-and-delete_with_value][Method ~#delete_with_value~ and ~#delete_with_value!~]]
|
|
55
|
-
- [[#method-keys_with_value][Method ~#keys_with_value~]]
|
|
56
|
-
- [[#method-remap_keys][Method ~#remap_keys~]]
|
|
57
|
-
- [[#method-replace_keys][Method ~#replace_keys~]]
|
|
58
|
-
- [[#alias-merge-to-][Alias ~#merge~ to ~<<~]]
|
|
59
|
-
- [[#numeric][Numeric]]
|
|
60
|
-
- [[#method-signum][Method ~#signum~]]
|
|
61
|
-
- [[#method-commasplaces--nil][Method ~#commas(places = nil)~]]
|
|
62
|
-
- [[#methods-whole-and-int_if_whole][Methods ~#whole?~ and ~#int_if_whole~]]
|
|
63
|
-
- [[#method-secs_to_hms][Method ~#secs_to_hms~]]
|
|
64
|
-
- [[#range][Range]]
|
|
65
|
-
- [[#methods-contiguous-left_contiguous-right_contiguous][Methods ~#contiguous~, ~#left_contiguous~, ~#right_contiguous~]]
|
|
66
|
-
- [[#method-joinother][Method ~#join(other)~]]
|
|
67
|
-
- [[#method-spanned_byothers][Method ~#spanned_by?(others)~]]
|
|
68
|
-
- [[#methods-gapsothers-overlapsothers][Methods ~#gaps(others)~, ~#overlaps(others)~]]
|
|
69
|
-
- [[#string][String]]
|
|
70
|
-
- [[#method-fuzzy_match][Method ~#fuzzy_match~]]
|
|
71
|
-
- [[#method-matches_with][Method ~#matches_with~]]
|
|
72
|
-
- [[#method-entitle][Method ~#entitle~]]
|
|
73
|
-
- [[#method-distance][Method ~#distance~]]
|
|
74
|
-
- [[#method-commasplaces][Method ~#commas(places)~]]
|
|
75
|
-
- [[#method-wrapwidth-hang][Method =#wrap(width, hang)=]]
|
|
76
|
-
- [[#method-as_sym][Method =#as_sym=]]
|
|
77
|
-
- [[#symbol][Symbol]]
|
|
78
|
-
- [[#method-as_str][Method =#as_str=]]
|
|
79
|
-
- [[#tex-quoting][TeX Quoting]]
|
|
80
|
-
- [[#contributing][Contributing]]
|
|
10
|
+
#+BEGIN_EXPORT markdown
|
|
11
|
+
[](https://github.com/ddoherty03/fat_core/actions/workflows/ruby.yml)
|
|
12
|
+
#+END_EXPORT
|
|
81
13
|
|
|
82
14
|
* Version
|
|
83
15
|
#+begin_src ruby
|
|
84
|
-
require_relative './lib/fat_core/version'
|
|
85
16
|
"Current version is: #{FatCore::VERSION}"
|
|
86
17
|
#+end_src
|
|
87
18
|
|
|
19
|
+
#+RESULTS:
|
|
88
20
|
#+begin_example
|
|
89
|
-
Current version is: 7.
|
|
21
|
+
Current version is: 7.1.3
|
|
90
22
|
#+end_example
|
|
91
23
|
|
|
92
|
-
*
|
|
93
|
-
|
|
24
|
+
* Introduction
|
|
94
25
|
~fat-core~ is somewhat of a grab bag of core class extensions that I have
|
|
95
26
|
found useful across several projects. It's higgeldy-piggeldy nature reflects
|
|
96
27
|
the fact that none of them are important enough to deserve a gem of their own,
|
|
@@ -101,19 +32,19 @@ projects and provide a focused place to develop and test them.
|
|
|
101
32
|
|
|
102
33
|
Add this line to your application's Gemfile:
|
|
103
34
|
|
|
104
|
-
#+begin_src ruby
|
|
35
|
+
#+begin_src ruby :eval no
|
|
105
36
|
gem 'fat_core'
|
|
106
37
|
#+end_SRC
|
|
107
38
|
|
|
108
39
|
And then execute:
|
|
109
40
|
|
|
110
|
-
#+begin_src
|
|
41
|
+
#+begin_src sh
|
|
111
42
|
$ bundle
|
|
112
43
|
#+end_src
|
|
113
44
|
|
|
114
45
|
Or install it yourself as:
|
|
115
46
|
|
|
116
|
-
#+begin_src
|
|
47
|
+
#+begin_src sh
|
|
117
48
|
$ gem install fat_core
|
|
118
49
|
#+end_src
|
|
119
50
|
|
|
@@ -121,7 +52,7 @@ Or install it yourself as:
|
|
|
121
52
|
|
|
122
53
|
You can extend classes individually by requiring the corresponding file:
|
|
123
54
|
|
|
124
|
-
#+begin_SRC ruby
|
|
55
|
+
#+begin_SRC ruby :eval no
|
|
125
56
|
require 'fat_core/array'
|
|
126
57
|
require 'fat_core/bigdecimal'
|
|
127
58
|
require 'fat_core/enumerable'
|
|
@@ -139,11 +70,16 @@ Or, you can require them all:
|
|
|
139
70
|
require 'fat_core/all'
|
|
140
71
|
#+end_SRC
|
|
141
72
|
|
|
73
|
+
#+RESULTS:
|
|
74
|
+
#+begin_example
|
|
75
|
+
false
|
|
76
|
+
#+end_example
|
|
77
|
+
|
|
142
78
|
Many of these have little that is of general interest, but there are a few
|
|
143
79
|
goodies.
|
|
144
80
|
|
|
145
|
-
|
|
146
|
-
|
|
81
|
+
** Array
|
|
82
|
+
*** Method ~#comma_join(sep: nil, last_sep: nil, two_sep: nil)~
|
|
147
83
|
Convert this array into a single string by (1) applying ~#to_s~ to each
|
|
148
84
|
element and (2) joining the elements with the string given by the ~sep:~
|
|
149
85
|
parameter. By default the sep parameter is ', '.
|
|
@@ -165,21 +101,19 @@ default.
|
|
|
165
101
|
If the input array is empty, ~#comma_join~ returns an empty string.
|
|
166
102
|
|
|
167
103
|
#+begin_src ruby
|
|
168
|
-
require_relative 'lib/fat_core/array'
|
|
169
|
-
|
|
170
104
|
%w{hammers nails glue bolts}.comma_join
|
|
171
105
|
#+end_src
|
|
172
106
|
|
|
107
|
+
#+RESULTS:
|
|
173
108
|
#+begin_example
|
|
174
109
|
hammers, nails, glue, and bolts
|
|
175
110
|
#+end_example
|
|
176
111
|
|
|
177
112
|
#+begin_src ruby
|
|
178
|
-
require_relative 'lib/fat_core/array'
|
|
179
|
-
|
|
180
113
|
%w{hammers nails}.comma_join
|
|
181
114
|
#+end_src
|
|
182
115
|
|
|
116
|
+
#+RESULTS:
|
|
183
117
|
#+begin_example
|
|
184
118
|
hammers and nails
|
|
185
119
|
#+end_example
|
|
@@ -187,74 +121,69 @@ hammers and nails
|
|
|
187
121
|
And, if you are ideologically opposed to the Oxford comma:
|
|
188
122
|
|
|
189
123
|
#+begin_src ruby
|
|
190
|
-
require_relative 'lib/fat_core/array'
|
|
191
|
-
|
|
192
124
|
%w{hammers nails glue bolts}.comma_join(last_sep: ' and ')
|
|
193
125
|
#+end_src
|
|
194
126
|
|
|
127
|
+
#+RESULTS:
|
|
195
128
|
#+begin_example
|
|
196
129
|
hammers, nails, glue and bolts
|
|
197
130
|
#+end_example
|
|
198
131
|
|
|
199
|
-
|
|
132
|
+
*** Method ~#last_i~
|
|
200
133
|
Return the index of the last element of the Array.
|
|
201
134
|
|
|
202
135
|
#+begin_src ruby
|
|
203
|
-
require_relative 'lib/fat_core/array'
|
|
204
|
-
|
|
205
136
|
%w{hammers nails glue bolts}.last_i
|
|
206
137
|
#+end_src
|
|
207
138
|
|
|
139
|
+
#+RESULTS:
|
|
208
140
|
#+begin_example
|
|
209
141
|
3
|
|
210
142
|
#+end_example
|
|
211
143
|
|
|
212
|
-
|
|
144
|
+
*** Method ~#intersect_with_dups~
|
|
213
145
|
Return a new Array that is the intersection of this Array with all ~others~,
|
|
214
146
|
but without removing duplicates as the ~Array#&~ method does. All items of
|
|
215
147
|
this Array are included in the result but only if they also appear in all of
|
|
216
148
|
the other Arrays.
|
|
217
149
|
|
|
218
150
|
#+begin_src ruby
|
|
219
|
-
require_relative 'lib/fat_core/array'
|
|
220
|
-
|
|
221
151
|
a = %w{hammers nails glue bolts nails}
|
|
222
|
-
b = %w{nails fingers knuckles nails}
|
|
152
|
+
b = %w{nails fingers bolts knuckles nails}
|
|
223
153
|
a.intersect_with_dups(b)
|
|
224
154
|
#+end_src
|
|
225
155
|
|
|
156
|
+
#+RESULTS:
|
|
226
157
|
#+begin_example
|
|
227
|
-
| nails | nails |
|
|
158
|
+
| nails | bolts | nails |
|
|
228
159
|
#+end_example
|
|
229
160
|
|
|
230
|
-
|
|
161
|
+
*** Method ~diff_with_dups~
|
|
231
162
|
Return an Array that is the difference between this Array and =other=, but
|
|
232
163
|
without removing duplicates as the Array#- method does. All items of this
|
|
233
164
|
Array are included in the result /unless/ they also appear in any of the
|
|
234
165
|
=other= Arrays.
|
|
235
166
|
|
|
236
167
|
#+begin_src ruby
|
|
237
|
-
require_relative 'lib/fat_core/array'
|
|
238
|
-
|
|
239
168
|
a = %w{hammers nails glue bolts hammers nails}
|
|
240
169
|
b = %w{nails fingers knuckles nails}
|
|
241
170
|
a.diff_with_dups(b)
|
|
242
171
|
#+end_src
|
|
243
172
|
|
|
173
|
+
#+RESULTS:
|
|
244
174
|
#+begin_example
|
|
245
175
|
| hammers | glue | bolts | hammers |
|
|
246
176
|
#+end_example
|
|
247
177
|
|
|
248
|
-
|
|
178
|
+
** BigDecimal ~#inspect~
|
|
249
179
|
~FatCore~ provides nothing but a better ~#inspect~ method for the ~BigDecimal~
|
|
250
180
|
class since the default inspect method is not very readable.
|
|
251
181
|
|
|
252
182
|
#+begin_src ruby
|
|
253
|
-
require_relative 'lib/fat_core/bigdecimal'
|
|
254
|
-
|
|
255
183
|
BigDecimal('2.1718281828').inspect
|
|
256
184
|
#+end_src
|
|
257
185
|
|
|
186
|
+
#+RESULTS:
|
|
258
187
|
#+begin_example
|
|
259
188
|
2.1718281828
|
|
260
189
|
#+end_example
|
|
@@ -262,8 +191,8 @@ class since the default inspect method is not very readable.
|
|
|
262
191
|
Without ~FatCore~, the result is "0.2718281828e1", forcing you to interpret
|
|
263
192
|
the exponent to understand where the decimal place is.
|
|
264
193
|
|
|
265
|
-
|
|
266
|
-
|
|
194
|
+
** Enumerable
|
|
195
|
+
*** Method ~#each_with_flags~
|
|
267
196
|
~FatCore::Enumerable~ extends ~Enumerable~ with the ~#each_with_flags~ method
|
|
268
197
|
that yields the elements of the ~Enumerable~ but also yields two booleans,
|
|
269
198
|
~first~ and ~last~ that are set to ~true~ on respectively, the first and last
|
|
@@ -271,8 +200,6 @@ element of the Enumerable and ~false~ otherwise. This makes it easy to treat
|
|
|
271
200
|
these two cases specially without testing the index as in ~#each_with_index~.
|
|
272
201
|
|
|
273
202
|
#+begin_src ruby
|
|
274
|
-
require_relative 'lib/fat_core/enumerable'
|
|
275
|
-
|
|
276
203
|
result = []
|
|
277
204
|
fibs = %w{1, 1, 2, 3, 5, 8, 13, 21}
|
|
278
205
|
fibs.each_with_flags do |f, first, last|
|
|
@@ -288,6 +215,7 @@ these two cases specially without testing the index as in ~#each_with_index~.
|
|
|
288
215
|
result
|
|
289
216
|
#+end_src
|
|
290
217
|
|
|
218
|
+
#+RESULTS:
|
|
291
219
|
#+begin_example
|
|
292
220
|
| Start | 1, |
|
|
293
221
|
| Continue | 1, |
|
|
@@ -299,17 +227,15 @@ these two cases specially without testing the index as in ~#each_with_index~.
|
|
|
299
227
|
| Last | 21 |
|
|
300
228
|
#+end_example
|
|
301
229
|
|
|
302
|
-
|
|
230
|
+
** Hash
|
|
303
231
|
FatCore::Hash extends the Hash class with some useful methods.
|
|
304
232
|
|
|
305
|
-
|
|
233
|
+
*** Method ~#each_pair_with_flags~
|
|
306
234
|
As with the extension for ~Enumerables~, ~FatCore~ provides a method for
|
|
307
235
|
enumerating the key-value pair of the ~Hash~ with flags that are set ~true~
|
|
308
236
|
for the first and last elements but ~false~ otherwise:
|
|
309
237
|
|
|
310
238
|
#+begin_src ruby
|
|
311
|
-
require_relative './lib/fat_core/hash'
|
|
312
|
-
|
|
313
239
|
h = {'Chaucer' => 'Cantebury Tales', 'Shakespeare' => 'The Merchant of Venice',
|
|
314
240
|
'Austen' => 'Pride and Prejudice', 'C. Brontë' => 'Jane Eyre',
|
|
315
241
|
'E. Brontë' => 'Wuthering Heights' }
|
|
@@ -330,6 +256,7 @@ for the first and last elements but ~false~ otherwise:
|
|
|
330
256
|
result
|
|
331
257
|
#+end_src
|
|
332
258
|
|
|
259
|
+
#+RESULTS:
|
|
333
260
|
#+begin_example
|
|
334
261
|
| Position | Author | Novel |
|
|
335
262
|
|----------+-------------+------------------------|
|
|
@@ -340,18 +267,17 @@ for the first and last elements but ~false~ otherwise:
|
|
|
340
267
|
| End | E. Brontë | Wuthering Heights |
|
|
341
268
|
#+end_example
|
|
342
269
|
|
|
343
|
-
|
|
270
|
+
*** Method ~#delete_with_value~ and ~#delete_with_value!~
|
|
344
271
|
This method modifies a ~Hash~ by deleting the key-value pairs when the value
|
|
345
272
|
equals the given value or values:
|
|
346
273
|
|
|
347
274
|
#+begin_src ruby :results output
|
|
348
|
-
require_relative './lib/fat_core/hash'
|
|
349
|
-
|
|
350
275
|
h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
|
|
351
276
|
h.delete_with_value!(2)
|
|
352
277
|
puts h
|
|
353
278
|
#+end_src
|
|
354
279
|
|
|
280
|
+
#+RESULTS:
|
|
355
281
|
#+begin_example
|
|
356
282
|
{:a=>1, :c=>3, :e=>1}
|
|
357
283
|
#+end_example
|
|
@@ -359,13 +285,12 @@ equals the given value or values:
|
|
|
359
285
|
You can supply multiple values for deletion:
|
|
360
286
|
|
|
361
287
|
#+begin_src ruby :results output
|
|
362
|
-
require_relative './lib/fat_core/hash'
|
|
363
|
-
|
|
364
288
|
h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
|
|
365
289
|
h.delete_with_value!(1, 3)
|
|
366
290
|
puts h
|
|
367
291
|
#+end_src
|
|
368
292
|
|
|
293
|
+
#+RESULTS:
|
|
369
294
|
#+begin_example
|
|
370
295
|
{:b=>2, :d=>2}
|
|
371
296
|
#+end_example
|
|
@@ -373,91 +298,94 @@ You can supply multiple values for deletion:
|
|
|
373
298
|
The non-bang method returns a clone of the Hash with the given deletions made:
|
|
374
299
|
|
|
375
300
|
#+begin_src ruby :results output
|
|
376
|
-
require_relative './lib/fat_core/hash'
|
|
377
|
-
|
|
378
301
|
h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
|
|
379
302
|
h2 = h.delete_with_value(1, 3)
|
|
380
303
|
puts h
|
|
381
304
|
puts h2
|
|
382
305
|
#+end_src
|
|
383
306
|
|
|
307
|
+
#+RESULTS:
|
|
384
308
|
#+begin_example
|
|
385
309
|
{:a=>1, :b=>2, :c=>3, :d=>2, :e=>1}
|
|
386
310
|
{:b=>2, :d=>2}
|
|
387
311
|
#+end_example
|
|
388
312
|
|
|
389
|
-
|
|
313
|
+
*** Method ~#keys_with_value~
|
|
390
314
|
Return an ~Array~ of keys of the ~Hash~ with a value ~==~ to the given value
|
|
391
315
|
or values.
|
|
392
316
|
|
|
393
|
-
#+begin_src ruby :results
|
|
394
|
-
|
|
317
|
+
#+begin_src ruby :results value code
|
|
318
|
+
h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
|
|
319
|
+
h.keys_with_value(1).inspect
|
|
320
|
+
#+end_src
|
|
395
321
|
|
|
322
|
+
#+RESULTS:
|
|
323
|
+
#+begin_example
|
|
324
|
+
"[:a, :e]"
|
|
325
|
+
#+end_example
|
|
326
|
+
|
|
327
|
+
#+begin_src ruby :results value code
|
|
396
328
|
h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
|
|
397
|
-
|
|
398
|
-
puts h.keys_with_value(2, 3).inspect
|
|
329
|
+
h.keys_with_value(2, 3).inspect
|
|
399
330
|
#+end_src
|
|
400
331
|
|
|
332
|
+
#+RESULTS:
|
|
401
333
|
#+begin_example
|
|
402
|
-
[:
|
|
403
|
-
[:b, :d, :c]
|
|
334
|
+
"[:b, :d, :c]"
|
|
404
335
|
#+end_example
|
|
405
336
|
|
|
406
|
-
|
|
337
|
+
*** Method ~#remap_keys~
|
|
407
338
|
This method pre-dates the new ~#transform_keys~ method now available for
|
|
408
339
|
~Hash~, but it is kept as an alternative. It takes a ~Hash~ as an argument
|
|
409
340
|
that maps existing keys to their replacement in the resulting ~Hash~. The
|
|
410
341
|
original ~Hash~ is not effected.
|
|
411
342
|
|
|
412
|
-
#+begin_src ruby :results
|
|
343
|
+
#+begin_src ruby :results value code
|
|
413
344
|
require_relative './lib/fat_core/hash'
|
|
414
345
|
|
|
415
346
|
h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
|
|
416
|
-
|
|
347
|
+
h.remap_keys({:a => :A, :b => :B}).inspect
|
|
417
348
|
#+end_src
|
|
418
349
|
|
|
350
|
+
#+RESULTS:
|
|
419
351
|
#+begin_example
|
|
420
|
-
{:A=>1, :B=>2, :c=>3, :d=>2, :e=>1}
|
|
352
|
+
"{:A=>1, :B=>2, :c=>3, :d=>2, :e=>1}"
|
|
421
353
|
#+end_example
|
|
422
354
|
|
|
423
355
|
These days, a more systematic job could be done with ~#transform_keys~:
|
|
424
|
-
#+begin_src ruby :results
|
|
356
|
+
#+begin_src ruby :results value code
|
|
425
357
|
h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
|
|
426
|
-
|
|
358
|
+
h.transform_keys { |k| k.to_s.upcase.to_sym }.inspect
|
|
427
359
|
#+end_src
|
|
428
360
|
|
|
361
|
+
#+RESULTS:
|
|
429
362
|
#+begin_example
|
|
430
|
-
{:A=>1, :B=>2, :C=>3, :D=>2, :E=>1}
|
|
363
|
+
"{:A=>1, :B=>2, :C=>3, :D=>2, :E=>1}"
|
|
431
364
|
#+end_example
|
|
432
365
|
|
|
433
|
-
|
|
366
|
+
*** Method ~#replace_keys~
|
|
434
367
|
A wholesale replacement of the existing keys can be done with this method:
|
|
435
368
|
|
|
436
|
-
#+begin_src ruby :results
|
|
437
|
-
require_relative './lib/fat_core/hash'
|
|
438
|
-
|
|
369
|
+
#+begin_src ruby :results value code
|
|
439
370
|
h = { a: 1, b: 2, c: 3, d: 2, e: 1 }
|
|
440
|
-
|
|
441
|
-
puts h.replace_keys([:z, :y, :x, :w, :v]).inspect
|
|
371
|
+
h.replace_keys([:z, :y, :x, :w, :v]).inspect
|
|
442
372
|
#+end_src
|
|
443
373
|
|
|
374
|
+
#+RESULTS:
|
|
444
375
|
#+begin_example
|
|
445
|
-
{:
|
|
446
|
-
{:z=>1, :y=>2, :x=>3, :w=>2, :v=>1}
|
|
376
|
+
"{:z=>1, :y=>2, :x=>3, :w=>2, :v=>1}"
|
|
447
377
|
#+end_example
|
|
448
378
|
|
|
449
|
-
|
|
379
|
+
*** Alias ~#merge~ to ~<<~
|
|
450
380
|
Finally, ~FatCore~ adds the "shovel" operator as an alias for ~#merge~ to
|
|
451
381
|
provide a pretty way to represent the merger of the right ~Hash~ into the left
|
|
452
382
|
~Hash~:
|
|
453
383
|
|
|
454
|
-
#+begin_src ruby :results
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
h = {a: 'A', b: 'B', c: 'C'} << {c: 'CC', d: 'DD'} << {d: 'DDD', e: 'EEE'}
|
|
458
|
-
puts h
|
|
384
|
+
#+begin_src ruby :results value code
|
|
385
|
+
{a: 'A', b: 'B', c: 'C'} << {c: 'CC', d: 'DD'} << {d: 'DDD', e: 'EEE'}
|
|
459
386
|
#+end_src
|
|
460
387
|
|
|
388
|
+
#+RESULTS:
|
|
461
389
|
#+begin_example
|
|
462
390
|
{:a=>"A", :b=>"B", :c=>"CC", :d=>"DDD", :e=>"EEE"}
|
|
463
391
|
#+end_example
|
|
@@ -466,9 +394,8 @@ It groups values into pairs and applies the ~#to_h~ method to the right-hand
|
|
|
466
394
|
argument if it is an ~Enumerable~, so it also works if the right-hand argument
|
|
467
395
|
is an ~Array~ or ~Enumerable~:
|
|
468
396
|
|
|
469
|
-
#+begin_src ruby :results
|
|
397
|
+
#+begin_src ruby :results value code
|
|
470
398
|
require 'fileutils'
|
|
471
|
-
require_relative './lib/fat_core/hash'
|
|
472
399
|
|
|
473
400
|
FileUtils.mkdir_p('./tmp')
|
|
474
401
|
ff = File.open('./tmp/junk', 'w')
|
|
@@ -480,27 +407,40 @@ is an ~Array~ or ~Enumerable~:
|
|
|
480
407
|
{d: 'DDD', e: 'EEE'} <<
|
|
481
408
|
ff.readlines.map(&:chomp) <<
|
|
482
409
|
[[:h, 'HHHHH'], [:j, 'JJJJJ']]
|
|
483
|
-
# h.transform_keys!(&:to_sym)
|
|
484
410
|
ff.close
|
|
485
411
|
FileUtils.rm_rf('./tmp/junk')
|
|
486
|
-
|
|
412
|
+
h
|
|
487
413
|
#+end_src
|
|
488
414
|
|
|
415
|
+
#+RESULTS:
|
|
489
416
|
#+begin_example
|
|
490
417
|
{:a=>"A", :b=>"B", :c=>"CC", :d=>"DDD", :e=>"EEE", "f"=>"FFFF", "g"=>"GGGG", :h=>"HHHHH", :j=>"JJJJJ"}
|
|
491
418
|
#+end_example
|
|
492
419
|
|
|
493
|
-
|
|
494
|
-
|
|
420
|
+
** Numeric
|
|
421
|
+
FatCore::Numeric has methods for inserting grouping commas into a number
|
|
422
|
+
(~#commas~ and ~#group~), for converting seconds to HH:MM:SS.dd format
|
|
423
|
+
(~#secs_to_hms~), for testing for integrality (~#whole?~ and ~#int_if_whole~),
|
|
424
|
+
and testing for sign (~#signum~).
|
|
425
|
+
|
|
426
|
+
*** Method ~#signum~
|
|
495
427
|
Return ~-1~ for negative numbers, ~0~ for zero, and ~+1~ for positive numbers.
|
|
496
428
|
This is sometimes handy.
|
|
497
429
|
|
|
498
|
-
|
|
430
|
+
#+begin_src ruby :results value code
|
|
431
|
+
[-55.signum, 0.signum, 55.signum]
|
|
432
|
+
#+end_src
|
|
433
|
+
|
|
434
|
+
#+RESULTS:
|
|
435
|
+
#+begin_example
|
|
436
|
+
[-1, 0, 1]
|
|
437
|
+
#+end_example
|
|
438
|
+
|
|
439
|
+
*** Method ~#commas(places = nil)~
|
|
499
440
|
To get s ~String~ representation of a ~Numeric~ with grouping commas inserted,
|
|
500
441
|
~FatCore~ provides the ~#commas~ method:
|
|
501
442
|
|
|
502
443
|
#+begin_src ruby
|
|
503
|
-
require_relative 'lib/fat_core/numeric'
|
|
504
444
|
result = []
|
|
505
445
|
result << ['N', 'Places', 'N.commas(places)']
|
|
506
446
|
result << nil
|
|
@@ -514,6 +454,7 @@ To get s ~String~ representation of a ~Numeric~ with grouping commas inserted,
|
|
|
514
454
|
result
|
|
515
455
|
#+end_src
|
|
516
456
|
|
|
457
|
+
#+RESULTS:
|
|
517
458
|
#+begin_example
|
|
518
459
|
| N | Places | N.commas(places) |
|
|
519
460
|
|--------------------+--------+--------------------------|
|
|
@@ -534,17 +475,11 @@ To get s ~String~ representation of a ~Numeric~ with grouping commas inserted,
|
|
|
534
475
|
| 16236565468798.668 | 5 | 16,236,565,468,798.66800 |
|
|
535
476
|
#+end_example
|
|
536
477
|
|
|
537
|
-
|
|
538
|
-
(~#commas~ and ~#group~), for converting seconds to HH:MM:SS.dd format
|
|
539
|
-
(~#secs_to_hms~), for testing for integrality (~#whole?~ and ~#int_if_whole~), and
|
|
540
|
-
testing for sign (~#signum~).
|
|
541
|
-
|
|
542
|
-
**** Methods ~#whole?~ and ~#int_if_whole~
|
|
478
|
+
*** Methods ~#whole?~ and ~#int_if_whole~
|
|
543
479
|
At times it is useful to know if a Float or BigDecimal can be converted to an
|
|
544
480
|
~Integer~ without losing precision.
|
|
545
481
|
|
|
546
482
|
#+begin_src ruby
|
|
547
|
-
require_relative 'lib/fat_core/numeric'
|
|
548
483
|
result = []
|
|
549
484
|
result << ['N', '#whole?', '#int_if_whole', 'Classes']
|
|
550
485
|
result << nil
|
|
@@ -555,6 +490,7 @@ At times it is useful to know if a Float or BigDecimal can be converted to an
|
|
|
555
490
|
result
|
|
556
491
|
#+end_src
|
|
557
492
|
|
|
493
|
+
#+RESULTS:
|
|
558
494
|
#+begin_example
|
|
559
495
|
| N | #whole? | #int_if_whole | Classes |
|
|
560
496
|
|--------------------+---------+--------------------+--------------------|
|
|
@@ -565,13 +501,12 @@ At times it is useful to know if a Float or BigDecimal can be converted to an
|
|
|
565
501
|
| 16236565468798.668 | false | 16236565468798.668 | Float -> Float |
|
|
566
502
|
#+end_example
|
|
567
503
|
|
|
568
|
-
|
|
504
|
+
*** Method ~#secs_to_hms~
|
|
569
505
|
This method converts a numeric representing a number of seconds or an angle in
|
|
570
506
|
degrees to a ~String~ of the form "HH:MM:SS" representing the same quantity in
|
|
571
|
-
hours, minutes, and seconds.
|
|
507
|
+
hours (or degrees), minutes, and seconds.
|
|
572
508
|
|
|
573
509
|
#+begin_src ruby
|
|
574
|
-
require_relative 'lib/fat_core/numeric'
|
|
575
510
|
result = []
|
|
576
511
|
result << ['N', 'HH:MM:SS']
|
|
577
512
|
result << nil
|
|
@@ -582,6 +517,7 @@ hours, minutes, and seconds.
|
|
|
582
517
|
result
|
|
583
518
|
#+end_src
|
|
584
519
|
|
|
520
|
+
#+RESULTS:
|
|
585
521
|
#+begin_example
|
|
586
522
|
| N | HH:MM:SS |
|
|
587
523
|
|-------------------+-------------|
|
|
@@ -592,8 +528,7 @@ hours, minutes, and seconds.
|
|
|
592
528
|
| 565.4861999999999 | 00:09:25.48 |
|
|
593
529
|
#+end_example
|
|
594
530
|
|
|
595
|
-
|
|
596
|
-
*** Range
|
|
531
|
+
** Range
|
|
597
532
|
~FatCore~ can also extend the Range class with several useful methods that
|
|
598
533
|
emphasize coverage of one range by one or more others (~#spanned_by?~ and
|
|
599
534
|
~#gaps~), contiguity of Ranges to one another (~#contiguous?~,
|
|
@@ -604,7 +539,7 @@ which combines fat_core's extended Range class with its extended Date class to
|
|
|
604
539
|
make a useful Period class for date ranges, and you may find fat_core's
|
|
605
540
|
extended Range class likewise useful.
|
|
606
541
|
|
|
607
|
-
|
|
542
|
+
*** Methods ~#contiguous~, ~#left_contiguous~, ~#right_contiguous~
|
|
608
543
|
These methods determine whether the subject ~Range~ are "contiguous" with
|
|
609
544
|
another ~Range~ on the left, right, or either side. The notion of contiguity
|
|
610
545
|
is different for ~Ranges~ whose min and max values respond to the ~#succ~
|
|
@@ -614,7 +549,6 @@ the max value of the left ~Range~ must equal the min value of the right
|
|
|
614
549
|
~Range~.
|
|
615
550
|
|
|
616
551
|
#+begin_src ruby
|
|
617
|
-
require_relative 'lib/fat_core/range'
|
|
618
552
|
require 'date'
|
|
619
553
|
|
|
620
554
|
result = []
|
|
@@ -637,6 +571,7 @@ the max value of the left ~Range~ must equal the min value of the right
|
|
|
637
571
|
result
|
|
638
572
|
#+end_src
|
|
639
573
|
|
|
574
|
+
#+RESULTS:
|
|
640
575
|
#+begin_example
|
|
641
576
|
| Self | Other | Contiguous? | Right? | Left? |
|
|
642
577
|
|------------------------+------------------------+-------------+--------+-------|
|
|
@@ -647,16 +582,15 @@ the max value of the left ~Range~ must equal the min value of the right
|
|
|
647
582
|
| 3.146..12.3 | 0.5..3.145 | false | false | false |
|
|
648
583
|
| a..q | r..z | true | true | false |
|
|
649
584
|
| a..q | s..z | false | false | false |
|
|
650
|
-
| 1963-11-22..1964-11-03 | 1964-11-04..
|
|
651
|
-
| 1963-11-22..1964-11-03 | 1964-11-28..
|
|
585
|
+
| 1963-11-22..1964-11-03 | 1964-11-04..2026-05-29 | true | true | false |
|
|
586
|
+
| 1963-11-22..1964-11-03 | 1964-11-28..2026-05-29 | false | false | false |
|
|
652
587
|
#+end_example
|
|
653
588
|
|
|
654
|
-
|
|
589
|
+
*** Method ~#join(other)~
|
|
655
590
|
If ~self~ is contiguous with ~other~, return a new ~Range~ that splices the
|
|
656
591
|
two ~Range~s into one ~Range~.
|
|
657
592
|
|
|
658
593
|
#+begin_src ruby
|
|
659
|
-
require_relative 'lib/fat_core/range'
|
|
660
594
|
require 'date'
|
|
661
595
|
|
|
662
596
|
result = []
|
|
@@ -679,6 +613,7 @@ two ~Range~s into one ~Range~.
|
|
|
679
613
|
result
|
|
680
614
|
#+end_src
|
|
681
615
|
|
|
616
|
+
#+RESULTS:
|
|
682
617
|
#+begin_example
|
|
683
618
|
| Self | Other | Contiguous? | Joined |
|
|
684
619
|
|------------------------+------------------------+-------------+------------------------|
|
|
@@ -689,12 +624,11 @@ two ~Range~s into one ~Range~.
|
|
|
689
624
|
| 3.146..12.3 | 0.5..3.145 | false | |
|
|
690
625
|
| a..q | r..z | true | a..z |
|
|
691
626
|
| a..q | s..z | false | |
|
|
692
|
-
| 1963-11-22..1964-11-03 | 1964-11-04..
|
|
693
|
-
| 1963-11-22..1964-11-03 | 1964-11-28..
|
|
627
|
+
| 1963-11-22..1964-11-03 | 1964-11-04..2026-05-29 | true | 1963-11-22..2026-05-29 |
|
|
628
|
+
| 1963-11-22..1964-11-03 | 1964-11-28..2026-05-29 | false | |
|
|
694
629
|
#+end_example
|
|
695
630
|
|
|
696
|
-
|
|
697
|
-
**** Method ~#spanned_by?(others)~
|
|
631
|
+
*** Method ~#spanned_by?(others)~
|
|
698
632
|
A set of ~Ranges~ "spans" a given ~Range~ if the set is contiguous and fully
|
|
699
633
|
covers the given ~Range~ with no overlaps and no gaps. A set that over-covers
|
|
700
634
|
the given ~Range~ is still considered to span it, even though it is wider than
|
|
@@ -702,7 +636,6 @@ the given ~Range~. In other words, a set spans the given ~Range~ if the set
|
|
|
702
636
|
can be joined and the given ~Range~ is within the joined ~Range~.
|
|
703
637
|
|
|
704
638
|
#+begin_src ruby
|
|
705
|
-
require_relative 'lib/fat_core/range'
|
|
706
639
|
require 'date'
|
|
707
640
|
|
|
708
641
|
result = []
|
|
@@ -723,6 +656,7 @@ can be joined and the given ~Range~ is within the joined ~Range~.
|
|
|
723
656
|
result
|
|
724
657
|
#+end_src
|
|
725
658
|
|
|
659
|
+
#+RESULTS:
|
|
726
660
|
#+begin_example
|
|
727
661
|
| Self | Others | Spanned By? |
|
|
728
662
|
|-------------+-------------------------------------+-------------|
|
|
@@ -735,14 +669,13 @@ can be joined and the given ~Range~ is within the joined ~Range~.
|
|
|
735
669
|
| a..z | ["a".."g", "j".."s", "t".."z"] | false |
|
|
736
670
|
#+end_example
|
|
737
671
|
|
|
738
|
-
|
|
672
|
+
*** Methods ~#gaps(others)~, ~#overlaps(others)~
|
|
739
673
|
When the set of other ~Ranges~ does not span the given ~Range~, these methods
|
|
740
674
|
return an set of ~Ranges~ that represent the portions of the given ~Range~ no
|
|
741
675
|
covered by the ~others~, the "gaps", or the points within the given ~Range~
|
|
742
676
|
where the ~others~ overlap one another and thus are not contiguous.
|
|
743
677
|
|
|
744
678
|
#+begin_src ruby
|
|
745
|
-
require_relative 'lib/fat_core/range'
|
|
746
679
|
require 'date'
|
|
747
680
|
|
|
748
681
|
result = []
|
|
@@ -764,6 +697,7 @@ where the ~others~ overlap one another and thus are not contiguous.
|
|
|
764
697
|
result
|
|
765
698
|
#+end_src
|
|
766
699
|
|
|
700
|
+
#+RESULTS:
|
|
767
701
|
#+begin_example
|
|
768
702
|
| Self | Others | Spanned By? | Gaps | Overlaps |
|
|
769
703
|
|-------------+-------------------------------------+-------------+--------------+----------|
|
|
@@ -777,7 +711,8 @@ where the ~others~ overlap one another and thus are not contiguous.
|
|
|
777
711
|
| a..z | ["a".."g", "j".."s", "t".."z"] | false | ["h".."i"] | [] |
|
|
778
712
|
#+end_example
|
|
779
713
|
|
|
780
|
-
|
|
714
|
+
** String
|
|
715
|
+
|
|
781
716
|
FatCore::String has methods for performing matching of one string with another
|
|
782
717
|
(~#matches_with~, ~#fuzzy_match~), for converting a string to title-case as
|
|
783
718
|
might by used in the title of a book (~#entitle~), for converting a String
|
|
@@ -786,7 +721,7 @@ into a useable Symbol (~#as_sym~) and vice-versa (~#as_str~ also
|
|
|
786
721
|
cleaning up errant spaces (~#clean~), and computing the Damerau-Levenshtein
|
|
787
722
|
distance between strings (~#distance~). And several others.
|
|
788
723
|
|
|
789
|
-
|
|
724
|
+
*** Method ~#fuzzy_match~
|
|
790
725
|
The ~#fuzzy_match~ method determines whether the subject string matches the
|
|
791
726
|
given "matcher" string, which provides a simple syntax that allows a limited
|
|
792
727
|
kind of pattern matching. If there is a match, it returns the matched portion
|
|
@@ -823,8 +758,6 @@ matcher both the space and colon ':' have special meaning as shown below.
|
|
|
823
758
|
9. Require each component to match some part of self, and
|
|
824
759
|
|
|
825
760
|
#+begin_src ruby
|
|
826
|
-
require_relative './lib/fat_core/string'
|
|
827
|
-
|
|
828
761
|
result = []
|
|
829
762
|
result << ['Self', 'Matcher', 'Match']
|
|
830
763
|
result << nil
|
|
@@ -836,6 +769,7 @@ matcher both the space and colon ':' have special meaning as shown below.
|
|
|
836
769
|
result
|
|
837
770
|
#+end_src
|
|
838
771
|
|
|
772
|
+
#+RESULTS:
|
|
839
773
|
#+begin_example
|
|
840
774
|
| Self | Matcher | Match |
|
|
841
775
|
|---------------------+----------------+----------------|
|
|
@@ -850,7 +784,7 @@ matcher both the space and colon ':' have special meaning as shown below.
|
|
|
850
784
|
| St. Luke's Hospital | lukes:hospital | Lukes Hospital |
|
|
851
785
|
#+end_example
|
|
852
786
|
|
|
853
|
-
|
|
787
|
+
*** Method ~#matches_with~
|
|
854
788
|
The ~#matches_with(matcher)~ method allows the use of either a regular
|
|
855
789
|
expression or fuzzy matching as described above depending on whether the
|
|
856
790
|
matcher is enclosed in '/' characters. It also returns the matched portion of
|
|
@@ -859,8 +793,6 @@ case insensitive by default and commas, apostrophes, and periods are removed
|
|
|
859
793
|
from the subject string before matching.
|
|
860
794
|
|
|
861
795
|
#+begin_src ruby
|
|
862
|
-
require_relative './lib/fat_core/string'
|
|
863
|
-
|
|
864
796
|
result = []
|
|
865
797
|
result << ['Self', 'Matcher', 'Match']
|
|
866
798
|
result << nil
|
|
@@ -872,20 +804,21 @@ from the subject string before matching.
|
|
|
872
804
|
result
|
|
873
805
|
#+end_src
|
|
874
806
|
|
|
807
|
+
#+RESULTS:
|
|
875
808
|
#+begin_example
|
|
876
|
-
| Self | Matcher | Match
|
|
877
|
-
|
|
878
|
-
| St. Luke's Hospital | st lukes | St Lukes
|
|
879
|
-
| St. Luke's Hospital | /luk.*hosp/ |
|
|
880
|
-
| St. Luke's Hospital | st:spital | nil
|
|
881
|
-
| St. Luke's Hospital | /u.*s\b/ |
|
|
882
|
-
| St. Luke's Hospital | st:laks | nil
|
|
883
|
-
| St. Luke's Hospital | :lukes | nil
|
|
884
|
-
| St. Luke's Hospital | s lukes | St Lukes
|
|
885
|
-
| St. Luke's Hospital | /lukes hospital\z/ |
|
|
809
|
+
| Self | Matcher | Match |
|
|
810
|
+
|---------------------+--------------------+-------------|
|
|
811
|
+
| St. Luke's Hospital | st lukes | St Lukes |
|
|
812
|
+
| St. Luke's Hospital | /luk.*hosp/ | Luke's Hosp |
|
|
813
|
+
| St. Luke's Hospital | st:spital | nil |
|
|
814
|
+
| St. Luke's Hospital | /u.*s\b/ | uke's |
|
|
815
|
+
| St. Luke's Hospital | st:laks | nil |
|
|
816
|
+
| St. Luke's Hospital | :lukes | nil |
|
|
817
|
+
| St. Luke's Hospital | s lukes | St Lukes |
|
|
818
|
+
| St. Luke's Hospital | /lukes hospital\z/ | nil |
|
|
886
819
|
#+end_example
|
|
887
820
|
|
|
888
|
-
|
|
821
|
+
*** Method ~#entitle~
|
|
889
822
|
For a string meant to serve as the title of a book, song, or other item, there
|
|
890
823
|
are certain rules in English as to which words should be capitalized and which
|
|
891
824
|
should be put in lower case. "PROFILES IN courage" should be rendered
|
|
@@ -894,8 +827,6 @@ capitalized unless it starts the title: "in the HEAT OF THE NIght" should be
|
|
|
894
827
|
something like "In the Heat of the Night".
|
|
895
828
|
|
|
896
829
|
#+begin_src ruby
|
|
897
|
-
require_relative './lib/fat_core/string'
|
|
898
|
-
|
|
899
830
|
result = []
|
|
900
831
|
result << ['Self', 'Entitled']
|
|
901
832
|
result << nil
|
|
@@ -907,6 +838,7 @@ something like "In the Heat of the Night".
|
|
|
907
838
|
result
|
|
908
839
|
#+end_src
|
|
909
840
|
|
|
841
|
+
#+RESULTS:
|
|
910
842
|
#+begin_example
|
|
911
843
|
| Self | Entitled |
|
|
912
844
|
|-------------------------------+-------------------------------|
|
|
@@ -917,15 +849,12 @@ something like "In the Heat of the Night".
|
|
|
917
849
|
| lucy in the sky with diamonds | Lucy in the Sky With Diamonds |
|
|
918
850
|
#+end_example
|
|
919
851
|
|
|
920
|
-
|
|
921
|
-
**** Method ~#distance~
|
|
852
|
+
*** Method ~#distance~
|
|
922
853
|
~FatCore~ provides ~distance~ as a simple wrapper around the
|
|
923
854
|
Damerau-Levenshtein distance function in ~damerau-levenshtein~ gem, using a
|
|
924
855
|
block size of 1 and a max distance of 10.
|
|
925
856
|
|
|
926
857
|
#+begin_src ruby
|
|
927
|
-
require_relative './lib/fat_core/string'
|
|
928
|
-
|
|
929
858
|
result = []
|
|
930
859
|
result << ['Word1', 'Word2', 'Distance']
|
|
931
860
|
result << nil
|
|
@@ -936,6 +865,7 @@ block size of 1 and a max distance of 10.
|
|
|
936
865
|
result
|
|
937
866
|
#+end_src
|
|
938
867
|
|
|
868
|
+
#+RESULTS:
|
|
939
869
|
#+begin_example
|
|
940
870
|
| Word1 | Word2 | Distance |
|
|
941
871
|
|---------+-----------+----------|
|
|
@@ -946,15 +876,13 @@ block size of 1 and a max distance of 10.
|
|
|
946
876
|
| Smith | Jones | 5 |
|
|
947
877
|
#+end_example
|
|
948
878
|
|
|
949
|
-
|
|
879
|
+
*** Method ~#commas(places)~
|
|
950
880
|
When presenting numbers, it is common to want to add grouping digits to make
|
|
951
881
|
the numbers more readable. The ~commas(places)~ method does this be
|
|
952
882
|
converting the number into a Float, rounding to places digits, then converting
|
|
953
883
|
back to a ~String~ with grouping commas inserted.
|
|
954
884
|
|
|
955
885
|
#+begin_src ruby
|
|
956
|
-
require_relative './lib/fat_core/string'
|
|
957
|
-
|
|
958
886
|
result = []
|
|
959
887
|
result << ['N', 'Places', 'With Commas']
|
|
960
888
|
result << nil
|
|
@@ -966,6 +894,7 @@ back to a ~String~ with grouping commas inserted.
|
|
|
966
894
|
result
|
|
967
895
|
#+end_src
|
|
968
896
|
|
|
897
|
+
#+RESULTS:
|
|
969
898
|
#+begin_example
|
|
970
899
|
| N | Places | With Commas |
|
|
971
900
|
|--------------------+--------+-------------------|
|
|
@@ -978,13 +907,10 @@ back to a ~String~ with grouping commas inserted.
|
|
|
978
907
|
| +3.14159e3 | 2 | 3,141.59 |
|
|
979
908
|
#+end_example
|
|
980
909
|
|
|
981
|
-
|
|
982
|
-
**** Method =#wrap(width, hang)=
|
|
910
|
+
*** Method ~#wrap(width, hang)~
|
|
983
911
|
This method wraps the string to a given width with an optional hanging indent
|
|
984
912
|
for lines after the first.
|
|
985
913
|
#+begin_src ruby
|
|
986
|
-
require_relative './lib/fat_core/string'
|
|
987
|
-
|
|
988
914
|
getty = <<~EOS
|
|
989
915
|
Four score and seven years ago our fathers brought forth on this continent,
|
|
990
916
|
a new nation, conceived in Liberty, and dedicated to the proposition that
|
|
@@ -1014,6 +940,7 @@ for lines after the first.
|
|
|
1014
940
|
getty.wrap(110, 3)
|
|
1015
941
|
#+end_src
|
|
1016
942
|
|
|
943
|
+
#+RESULTS:
|
|
1017
944
|
#+begin_example
|
|
1018
945
|
Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived
|
|
1019
946
|
in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged
|
|
@@ -1033,38 +960,153 @@ Four score and seven years ago our fathers brought forth on this continent, a ne
|
|
|
1033
960
|
the people, for the people, shall not perish from the earth.
|
|
1034
961
|
#+end_example
|
|
1035
962
|
|
|
1036
|
-
|
|
963
|
+
#+begin_src ruby
|
|
964
|
+
getty.wrap(40, 2)
|
|
965
|
+
#+end_src
|
|
966
|
+
|
|
967
|
+
#+RESULTS:
|
|
968
|
+
#+begin_example
|
|
969
|
+
Four score and seven years ago our
|
|
970
|
+
fathers brought forth on this continent,
|
|
971
|
+
a new nation, conceived in Liberty,
|
|
972
|
+
and dedicated to the proposition that
|
|
973
|
+
all men are created equal. Now we
|
|
974
|
+
are engaged in a great civil war,
|
|
975
|
+
testing whether that nation, or any
|
|
976
|
+
nation so conceived and so dedicated,
|
|
977
|
+
can long endure. We are met on a
|
|
978
|
+
great battle-field of that war. We
|
|
979
|
+
have come to dedicate a portion of
|
|
980
|
+
that field, as a final resting place
|
|
981
|
+
for those who here gave their lives
|
|
982
|
+
that that nation might live. It is
|
|
983
|
+
altogether fitting and proper that
|
|
984
|
+
we should do this. But, in a larger
|
|
985
|
+
sense, we can not dedicate---we can
|
|
986
|
+
not consecrate---we can not hallow---this
|
|
987
|
+
ground. The brave men, living and
|
|
988
|
+
dead, who struggled here, have consecrated
|
|
989
|
+
it, far above our poor power to
|
|
990
|
+
add or detract. The world will little
|
|
991
|
+
note, nor long remember what we say
|
|
992
|
+
here, but it can never forget what
|
|
993
|
+
they did here. It is for us the
|
|
994
|
+
living, rather, to be dedicated here
|
|
995
|
+
to the unfinished work which they
|
|
996
|
+
who fought here have thus far so
|
|
997
|
+
nobly advanced. It is rather for
|
|
998
|
+
us to be here dedicated to the great
|
|
999
|
+
task remaining before us---that from
|
|
1000
|
+
these honored dead we take increased
|
|
1001
|
+
devotion to that cause for which
|
|
1002
|
+
they gave the last full measure of
|
|
1003
|
+
devotion---that we here highly resolve
|
|
1004
|
+
that these dead shall not have died
|
|
1005
|
+
in vain---that this nation, under
|
|
1006
|
+
God, shall have a new birth of freedom---and
|
|
1007
|
+
that government of the people, by
|
|
1008
|
+
the people, for the people, shall
|
|
1009
|
+
not perish from the earth.
|
|
1010
|
+
#+end_example
|
|
1011
|
+
|
|
1012
|
+
*** Method ~#as_sym~
|
|
1037
1013
|
Convert a ~String~ to a ~Symbol~ by converting all letters to lower-case,
|
|
1038
1014
|
replacing hyphens and white space with a single underscore, and removing all
|
|
1039
1015
|
non-alphanumeric characters:
|
|
1040
1016
|
|
|
1041
1017
|
#+begin_src ruby
|
|
1042
|
-
require_relative './lib/fat_core/string'
|
|
1043
|
-
|
|
1044
1018
|
" Hello-to-the World!!!".as_sym
|
|
1045
1019
|
#+end_src
|
|
1046
1020
|
|
|
1021
|
+
#+RESULTS:
|
|
1047
1022
|
#+begin_example
|
|
1048
1023
|
:hello_to_the_world
|
|
1049
1024
|
#+end_example
|
|
1050
1025
|
|
|
1051
|
-
***
|
|
1052
|
-
|
|
1026
|
+
*** Method ~gut~
|
|
1027
|
+
Very often one wants to truncate a string to a given size, but it is often the
|
|
1028
|
+
case that the most important parts of a string are at the beginning and the
|
|
1029
|
+
end. The ~#gut(size)~ method returns a sting of length ~size~ by removing
|
|
1030
|
+
content from the middle of the string and leaving as much as possible at the
|
|
1031
|
+
two ends.
|
|
1032
|
+
|
|
1033
|
+
#+begin_src ruby
|
|
1034
|
+
result = []
|
|
1035
|
+
result << ['Self', 'Gutted']
|
|
1036
|
+
result << nil
|
|
1037
|
+
strings = ["/usr/bin/fallacious", "Class A Common Stock", 'PROFILES IN courage', 'in the HEAT OF THE NIght', 'a day in the life', 'FROM HERE TO ETERNITY',
|
|
1038
|
+
'lucy in the sky with diamonds']
|
|
1039
|
+
strings.each do |t|
|
|
1040
|
+
result << [t, t.gut(15)]
|
|
1041
|
+
end
|
|
1042
|
+
result
|
|
1043
|
+
#+end_src
|
|
1044
|
+
|
|
1045
|
+
#+RESULTS:
|
|
1046
|
+
#+begin_example
|
|
1047
|
+
| Self | Gutted |
|
|
1048
|
+
|-------------------------------+-----------------|
|
|
1049
|
+
| /usr/bin/fallacious | /usr/bi~lacious |
|
|
1050
|
+
| Class A Common Stock | Class A~n Stock |
|
|
1051
|
+
| PROFILES IN courage | PROFILE~courage |
|
|
1052
|
+
| in the HEAT OF THE NIght | in the ~E NIght |
|
|
1053
|
+
| a day in the life | a day i~he life |
|
|
1054
|
+
| FROM HERE TO ETERNITY | FROM HE~TERNITY |
|
|
1055
|
+
| lucy in the sky with diamonds | lucy in~iamonds |
|
|
1056
|
+
#+end_example
|
|
1057
|
+
|
|
1058
|
+
By default, the deleted middle part is replace with the tilde character, but
|
|
1059
|
+
with the ellipsis parameter, you can make it whatever you want:
|
|
1060
|
+
|
|
1061
|
+
#+begin_src ruby
|
|
1062
|
+
'Class A Common Stock'.gut(15, ellipsis: '...')
|
|
1063
|
+
#+end_src
|
|
1064
|
+
|
|
1065
|
+
#+RESULTS:
|
|
1066
|
+
#+begin_example
|
|
1067
|
+
Class ... Stock
|
|
1068
|
+
#+end_example
|
|
1069
|
+
|
|
1070
|
+
Including, nothing at all:
|
|
1071
|
+
|
|
1072
|
+
#+begin_src ruby
|
|
1073
|
+
'Class A Common Stock'.gut(15, ellipsis: '')
|
|
1074
|
+
#+end_src
|
|
1075
|
+
|
|
1076
|
+
#+RESULTS:
|
|
1077
|
+
#+begin_example
|
|
1078
|
+
Class A n Stock
|
|
1079
|
+
#+end_example
|
|
1080
|
+
|
|
1081
|
+
And, you can optionally squeeze out spaces or other characters with the
|
|
1082
|
+
|
|
1083
|
+
#+begin_src ruby
|
|
1084
|
+
'Class A Common Stock'.gut(16, ellipsis: '', squeeze: ' ')
|
|
1085
|
+
#+end_src
|
|
1086
|
+
|
|
1087
|
+
#+RESULTS:
|
|
1088
|
+
#+begin_example
|
|
1089
|
+
ClassAComonStock
|
|
1090
|
+
#+end_example
|
|
1091
|
+
|
|
1092
|
+
** Symbol
|
|
1093
|
+
|
|
1094
|
+
*** Method =#as_str=
|
|
1053
1095
|
A quasi-inverse of ~String#as_sym~, convert a ~Symbol~ into a ~String~ by
|
|
1054
1096
|
converting '_' to a hyphen, white-space into '_', and eliminate any
|
|
1055
1097
|
non-alphanumeric characters.
|
|
1056
1098
|
|
|
1057
1099
|
#+begin_src ruby
|
|
1058
|
-
require_relative 'lib/fat_core/symbol'
|
|
1059
|
-
|
|
1060
1100
|
:hello_to_the_world.as_str
|
|
1061
1101
|
#+end_src
|
|
1062
1102
|
|
|
1103
|
+
#+RESULTS:
|
|
1063
1104
|
#+begin_example
|
|
1064
1105
|
hello-to-the-world
|
|
1065
1106
|
#+end_example
|
|
1066
1107
|
|
|
1067
|
-
|
|
1108
|
+
** TeX Quoting
|
|
1109
|
+
|
|
1068
1110
|
The extensions for ~String~, ~Numeric~, ~Range~, ~Symbol~, and ~NilClass~
|
|
1069
1111
|
provide a ~#tex_quote~ method for quoting the string version of an object so
|
|
1070
1112
|
as to allow its inclusion in a TeX document while quoting characters such as
|
|
@@ -1073,7 +1115,6 @@ deploys TeX notation when special notation is available, for example, a
|
|
|
1073
1115
|
~Rational~ is rendered as a fraction.
|
|
1074
1116
|
|
|
1075
1117
|
#+begin_src ruby
|
|
1076
|
-
require_relative 'lib/fat_core/all'
|
|
1077
1118
|
require 'date'
|
|
1078
1119
|
|
|
1079
1120
|
result = []
|
|
@@ -1099,6 +1140,7 @@ deploys TeX notation when special notation is available, for example, a
|
|
|
1099
1140
|
result
|
|
1100
1141
|
#+end_src
|
|
1101
1142
|
|
|
1143
|
+
#+RESULTS:
|
|
1102
1144
|
#+begin_example
|
|
1103
1145
|
| Class | Example | #tex_quote |
|
|
1104
1146
|
|----------+---------------------------------------+-----------------------------------------------|
|
|
@@ -1110,13 +1152,12 @@ deploys TeX notation when special notation is available, for example, a
|
|
|
1110
1152
|
| Complex | 5.0+3.0i | "$5+3i$" |
|
|
1111
1153
|
| Rational | 5/3 | "$\\frac{5}{3}$" |
|
|
1112
1154
|
| Rational | 8/17 | "$\\frac{8}{17}$" |
|
|
1113
|
-
| Range | 2020-09-22..
|
|
1155
|
+
| Range | 2020-09-22..2026-05-29 | "(2020-09-22..2026-05-29)" |
|
|
1114
1156
|
| Range | 2.718281828459045..3.141592653589793 | "($e$..$\\pi$)" |
|
|
1115
1157
|
| Symbol | four_score_and_7_years | "four\\_score\\_and\\_7\\_years" |
|
|
1116
1158
|
| NilClass | | "" |
|
|
1117
1159
|
#+end_example
|
|
1118
1160
|
|
|
1119
|
-
|
|
1120
1161
|
* Contributing
|
|
1121
1162
|
|
|
1122
1163
|
1. Fork it ([[http://github.com/ddoherty03/fat_core/fork]] )
|