lite-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-leap-day
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'lite/ruby'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,284 @@
1
+ # Array
2
+
3
+ `assert_valid_values!`
4
+ ------
5
+ Raises an error if key is not included in a list of values.
6
+
7
+ ```ruby
8
+ {}.assert_valid_values!(:foo) #=> {}
9
+ { foo: 'bar' }.assert_valid_values!(:foo) #=> { foo: 'bar' }
10
+ { baz: 'boz' }.assert_valid_values!(:foo, :boo) #=> raises ArgumentError: 'Invalid value: ":baz". Allowed values are: ":foo", ":boo"'
11
+ ```
12
+
13
+ `assert_all_valid_values!`
14
+ ------
15
+ Raises like an error like `assert_valid_values!` but also on empty.
16
+
17
+ ```ruby
18
+ {}.assert_all_valid_values!(:foo) #=> raises ArgumentError: 'An empty array is not allowed'
19
+ ```
20
+
21
+ `after`
22
+ ------
23
+ Returns the value after a given value.
24
+
25
+ ```ruby
26
+ ['1', '2', '3'].after('2') #=> '3'
27
+ ['1', '2', '3'].after('3') #=> '1'
28
+ ['1', '2', '3'].after('4') #=> nil
29
+ ```
30
+
31
+ `bury`
32
+ ------
33
+ Updates a deeply nested value.
34
+
35
+ ```ruby
36
+ ['1', ['2']].bury(1, '3') #=> ['1', '3']
37
+ ['1', ['2']].bury(1, 0, '3') #=> ['1', ['3']]
38
+ ['1', ['2']].bury(1) #=> raises ArgumentError: '2 or more arguments required'
39
+ ```
40
+
41
+ `before`
42
+ ------
43
+ Returns the value before a given value.
44
+
45
+ ```ruby
46
+ ['1', '2', '3'].before('2') #=> '1'
47
+ ['1', '2', '3'].before('1') #=> '3'
48
+ ['1', '2', '3'].before('4') #=> nil
49
+ ```
50
+
51
+ `delete_first(!)`
52
+ ------
53
+ Removes the first element from an array. Like `shift`, but returns the array instead of the removed element.
54
+
55
+ ```ruby
56
+ ['1', '2', '3'].delete_first #=> ['2', '3']
57
+ ```
58
+
59
+ `delete_last(!)`
60
+ ------
61
+ Removes the last element from an array. Like `pop`, but returns the array instead of the removed element.
62
+
63
+ ```ruby
64
+ ['1', '2', '3'].delete_last #=> ['1', '2']
65
+ ```
66
+
67
+ `delete_values`
68
+ ------
69
+ Delete multiple values from array.
70
+
71
+ ```ruby
72
+ [1, 2, 3, 4].delete_values(1, 3) #=> [2, 4]
73
+ ```
74
+
75
+ `demote(!)`
76
+ ------
77
+ Moves a given value to the tail of array.
78
+
79
+ ```ruby
80
+ [1, 2, 2, 3].demote(2) #=> [1, 3, 2, 2]
81
+ [1, 2, 2, 3].demote!(4) #=> [1, 2, 2, 3]
82
+ ```
83
+
84
+ `denillify(!)`
85
+ ------
86
+ Converts `nil` into a given value.
87
+
88
+ ```ruby
89
+ [nil, 3, 4].denillify #=> [0, 3, 4]
90
+ [nil, 3, 4].denillify!(9) #=> [9, 3, 4]
91
+ ```
92
+
93
+ `duplicates`
94
+ ------
95
+ Returns a list of duplicate elements.
96
+
97
+ ```ruby
98
+ [1, 1, 2, 2, 2, 3].duplicates #=> [1, 2]
99
+ [1, 1, 2, 2, 2, 3].duplicates(3) #=> [2]
100
+ ```
101
+
102
+ `from`
103
+ ------
104
+ Returns the tail of the array from a given position.
105
+
106
+ ```ruby
107
+ ['1', '2', '3'].from(0) #=> ['1', '2', '3']
108
+ ['1', '2', '3'].from(1) #=> ['2', '3']
109
+ ['1', '2', '3'].from(-1) #=> ['3']
110
+ ```
111
+
112
+ `fulfill`
113
+ ------
114
+ Returns an array filled to given size.
115
+
116
+ ```ruby
117
+ ['1', '2'].fulfill('x', 4) #=> ['1', '2', 'x', 'x']
118
+ ```
119
+
120
+ `groups`
121
+ ------
122
+ Splits or iterates over the array to a given number of groups.
123
+
124
+ ```ruby
125
+ %w(1 2 3 4 5 6 7 8 9 10).groups(3) #=> [['1', '2', '3', '4'], ['5', '6', '7'], ['8', '9', '10']]
126
+ ```
127
+
128
+ `in_groups`
129
+ ------
130
+ Splits or iterates over the array to a given number of groups, padding any remaining slots with filler unless it is `false`.
131
+
132
+ ```ruby
133
+ %w(1 2 3 4 5 6 7 8 9 10).in_groups(3) #=> [['1', '2', '3', '4'], ['5', '6', '7', nil], ['8', '9', '10', nil]]
134
+ %w(1 2 3 4 5 6 7 8 9 10).in_groups(3, ' ') #=> [['1', '2', '3', '4'], ['5', '6', '7', ' '], ['8', '9', '10', ' ']]
135
+ %w(1 2 3 4 5 6 7 8 9 10).in_groups(3, false) #=> [['1', '2', '3', '4'], ['5', '6', '7'], ['8', '9', '10']]
136
+ ```
137
+
138
+ `in_groups_of`
139
+ ------
140
+ Splits or iterates over the array in groups of a given size number, padding any remaining slots with filler unless it is `false`.
141
+
142
+ ```ruby
143
+ %w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3) #=> [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10', nil, nil]]
144
+ %w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, ' ') #=> [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10', ' ', ' ']]
145
+ %w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, false) #=> [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10']]
146
+ ```
147
+
148
+ `indexes` aka `indicies`
149
+ ------
150
+ Returns all indexes of the matching value.
151
+
152
+ ```ruby
153
+ [:a,:b,:a,:c].indexes(:a) #=> [0, 2]
154
+ ```
155
+
156
+ `merge(!)`
157
+ ------
158
+ Concats multiple arrays.
159
+
160
+ ```ruby
161
+ [1, 2].merge([3, 4], [5, 6]) #=> [1, 2, 3, 4, 5, 6]
162
+ ```
163
+
164
+ `nillify(!)`
165
+ ------
166
+ Converts blank values into `nil`.
167
+
168
+ ```ruby
169
+ [nil, 3, 4].nillify #=> [nil, 3, 4]
170
+ [' ', 3, 4].nillify #=> [nil, 3, 4]
171
+ ['', 3, 4].nillify! #=> [nil, 3, 4]
172
+ ```
173
+
174
+ `probability`
175
+ ------
176
+ Generates a hash mapping each unique element in the array to the relative frequency, i.e. the probability, of it appearance.
177
+
178
+ ```ruby
179
+ [:a,:b,:c,:c].probability #=> { a: 0.25,b: 0.25,c: 0.5 }
180
+ ```
181
+
182
+ `promote(!)`
183
+ ------
184
+ Moves a given value to head of array.
185
+
186
+ ```ruby
187
+ [1, 2, 2, 3].promote(2) #=> [2, 2, 1, 3]
188
+ [1, 2, 2, 3].promote!(4) #=> [1, 2, 2, 3]
189
+ ```
190
+
191
+ `position`
192
+ ------
193
+ Returns the position of the first matching value.
194
+
195
+ ```ruby
196
+ [:a,:b,:a,:c].position(:a) #=> 1
197
+ ```
198
+
199
+ `positions`
200
+ ------
201
+ Returns all of the positions of the matching value.
202
+
203
+ ```ruby
204
+ [:a,:b,:a,:c].positions(:a) #=> [1, 3]
205
+ ```
206
+
207
+ `reject_values`
208
+ ------
209
+ Delete multiple values from a `dup` copy of the original array.
210
+
211
+ ```ruby
212
+ [1, 2, 3, 4, 5].reject_values(2, 4) #=> [1, 3, 5]
213
+ ```
214
+
215
+ `rposition`
216
+ ------
217
+ Returns the position of the last matching value.
218
+
219
+ ```ruby
220
+ [:a,:b,:a,:c].rposition(:a) #=> 3
221
+ ```
222
+
223
+ `sample!`
224
+ ------
225
+ Deletes a random value and returns that value.
226
+
227
+ ```ruby
228
+ [1, 2, 3, 4, 5].sample! #=> 2
229
+ ```
230
+
231
+ `split`
232
+ ------
233
+ Divides the array into one or more subarrays based on a delimiting value or the result of an optional block.
234
+
235
+ ```ruby
236
+ [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
237
+ (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
238
+ ```
239
+
240
+ `strip(!)`
241
+ ------
242
+ Removes blank elements from an array.
243
+
244
+ ```ruby
245
+ ['this', '', 'that', nil, false].strip #=> ['this', 'that']
246
+ 'this is a test'.split(' ').strip #=> ['this', 'is', 'a', 'test']
247
+ ```
248
+
249
+ `swap`
250
+ ------
251
+ Switches the places of two elements.
252
+
253
+ ```ruby
254
+ [1, 2, 3].swap(0, 2) #=> [3, 2, 1]
255
+ ```
256
+
257
+ `to`
258
+ ------
259
+ Returns the beginning of the array up to a given position.
260
+
261
+ ```ruby
262
+ ['1', '2', '3'].to(0) #=> ['1']
263
+ ['1', '2', '3'].to(1) #=> ['1', '2']
264
+ ['1', '2', '3'].to(-1) #=> ['3']
265
+ ```
266
+
267
+ `to_sentence`
268
+ ------
269
+ Converts the array to a comma-separated sentence where the last element is joined by the connector word.
270
+
271
+ Option | Type | Default
272
+ --- | --- | ---
273
+ words_connector | string | ', '
274
+ two_words_connector | string | ' and '
275
+ last_word_connector | string | ', and '
276
+
277
+ ```ruby
278
+ [].to_sentence #=> ''
279
+ ['one'].to_sentence #=> 'one'
280
+ ['one', 'two'].to_sentence #=> 'one and two'
281
+ ['one', 'two', 'three'].to_sentence #=> 'one,two,and three'
282
+ ['one', 'two'].to_sentence(two_words_connector: '-') #=> 'one-two'
283
+ ['one', 'two', 'three'].to_sentence(words_connector: ' or ',last_word_connector: ' or at least ') #=> 'one or two or at least three'
284
+ ```
@@ -0,0 +1,78 @@
1
+ # Date
2
+
3
+ `format`
4
+ ------
5
+ Converts a `date` object to `strftime` it using a human readable string.
6
+
7
+ | Directive | Type | Key | `strftime` | Result |
8
+ | --- | --- | --- | --- | --- |
9
+ | Weekday | Monday | `w`, `weekday` | %u | (1..7) |
10
+ | Weekday | Sunday | `ww`, `weekday_offset` | %w | (0..6) |
11
+ | Weekday | Name | `www`, `weekday_name` | %A | Sunday |
12
+ | Weekday | Name abbr | `wwww`, `weekday_name_abbr` | %a | Sun |
13
+ | Day | Zero-padded | `d`, `day`, `day_padded` | %d | (01..31) |
14
+ | Day | Unpadded | `dd`, `Day`, `day_unpadded` | %-d | (1..31) |
15
+ | Day | Blank-padded | `ddd`, `DAY`, `day_blank` | %_d | ( 1..31) |
16
+ | Day | Day of the year | `dddd`, `day_of_the_year` | %j | (001..366) |
17
+ | Month | Zero-padded | `m`, `month`, `month_padded` | %m | (01..12) |
18
+ | Month | Unpadded | `mm`, `Month`, `month_unpadded` | %-m | (1..12) |
19
+ | Month | Blank-padded | `mmm`, `MONTH`, `day_blank` | %_m | ( 1..12) |
20
+ | Month | Name | `mmmm`, `month_name` | %B | January |
21
+ | Month | Name abbr | `mmmmm`, `month_name_abbr` | %b | Jan |
22
+ | Week | ISO | `we`, `week` | %V | (00..53) |
23
+ | Week | Monday | `mwe`, `monday_week` | %W | (00..53) |
24
+ | Week | Sunday | `swe`, `sunday_week` | %U | (00..53) |
25
+ | Year | 2 Digits | `yy`, `yr` | %y | (00..99) |
26
+ | Year | 4 Digits | `yyyy`, `year` | %Y | 1999 |
27
+
28
+ ```ruby
29
+ Date.today.format('month_name day, year') #=> 'May 05, 2000'
30
+ ```
31
+
32
+ `stamp` aka `to_format`
33
+ ------
34
+ Converts a `date` object to a predefined format.
35
+
36
+ #### Base
37
+
38
+ | Directive | Type | Key | `strftime` | Result |
39
+ | --- | --- | --- | --- | --- |
40
+ | Weekday | Zero-padded | `weekday`, `:weekday_padded` | %d | (01..31) |
41
+ | Weekday | Blank-padded | `:weekday_blank` | %_d | ( 1..31) |
42
+ | Weekday | Unpadded | `:weekday_unpadded` | %-d | (1..31) |
43
+ | Weekday | Name | `:weekday_name` | %A | Sunday |
44
+ | Weekday | Name abbr | `:weekday_name_abbr` | %a | Sun |
45
+ | Month | Zero-padded | `:month`, `:month_padded` | %m | (01..12) |
46
+ | Month | Unpadded | `:month_unpadded` | %-m | (1..12) |
47
+ | Month | Blank-padded | `:month_blank` | %_m | ( 1..12) |
48
+ | Month | Name | `:month_name` | %B | January |
49
+ | Month | Name abbr | `:month_name_abbr` | %b | Jan |
50
+ | Week | ISO | `:week_iso` | %V | (00..53) |
51
+ | Week | Sunday | `:week_sunday` | %U | (00..53) |
52
+ | Week | Monday | `:week_monday` | %W | (00..53) |
53
+ | Year | 2 Digits | `:yr`, `:year_abbr` | %y | (00..99) |
54
+ | Year | 4 Digits | `:year` | %Y | 1999 |
55
+
56
+ #### Combinations
57
+
58
+ | Directive | Type | Key | `strftime` | Result |
59
+ | --- | --- | --- | --- | --- |
60
+ | Combo | Month day | `:day` | %B %-d | January 9 |
61
+ | Combo | Month day | `:day_abbr` | %b %-d | Jan 9 |
62
+ | Combo | Month day | `:day_iso` | %m-%d | 01-09 |
63
+ | Combo | Month year | `:month_year`, `:month_padded_year` | %m %Y | (01..12) 2015 |
64
+ | Combo | Month year | `:month_blank_year` | %_m %Y | ( 1..12) 2015 |
65
+ | Combo | Month year | `:month_unpadded_year` | %-m %Y | (1..12) 2015 |
66
+ | Combo | Month year | `:month_name_year` | %B %Y | January 2015 |
67
+ | Combo | Month year | `:month_name_abbr_year` | %b %Y | Jan 2015 |
68
+ | Combo | Week year | `:week_year_iso` | %V-%G | 04-2014 |
69
+ | Combo | Year day | `:year_day` | %Y-%m-%d | 1999-01-21 |
70
+ | Combo | Year week | `:year_week` | %G-%V | 1999-52 |
71
+ | Combo | Year month | `:year_month` | %Y-%m | 1999-01 |
72
+ | Combo | Date | `:date` | %B %-d, %Y | January 9, 2014 |
73
+ | Combo | Date | `:date_abbr` | %b %-d, %Y | Jan 9, 2014 |
74
+ | Combo | Date | `:date_iso` | %Y-%m-%d | 2014-01-09 |
75
+
76
+ ```ruby
77
+ Date.today.to_format(:year) #=> '2014'
78
+ ```
@@ -0,0 +1,174 @@
1
+ # Enumerable
2
+
3
+ `cluster`
4
+ ------
5
+ Groups together adjacent elements into a list of sub-arrays.
6
+
7
+ ```ruby
8
+ [2,2,2,3,3,4,2,2,1].cluster { |x| x } #=> [[2,2,2],[3,3],[4],[2,2],[1]]
9
+ ```
10
+
11
+ `deduce`
12
+ ------
13
+ Returns the difference of a collection of numbers.
14
+
15
+ ```ruby
16
+ [].deduce #=> 0
17
+ [].deduce(nil) #=> nil
18
+ [1,2,3].deduce #=> -4
19
+ ```
20
+
21
+ `divisible`
22
+ ------
23
+ Returns the division of a collection of numbers.
24
+
25
+ ```ruby
26
+ [].divisible #=> 0
27
+ [].divisible(nil) #=> nil
28
+ [16,4,2].divisible #=> 2
29
+ ```
30
+
31
+ `drop_last`
32
+ ------
33
+ Drops the last number of elements in a collection.
34
+
35
+ ```ruby
36
+ [].drop_last(1) #=> []
37
+ [1,2,3].drop_last(1) #=> [1,2]
38
+ [1,2,3].drop_last(2) #=> [1]
39
+ ```
40
+
41
+ `drop_last_if`
42
+ ------
43
+ Drops the last number of elements in a collection while it meets a criteria.
44
+
45
+ ```ruby
46
+ [].drop_last_if(&:odd?) #=> []
47
+ [1,2,3].drop_last_if(&:odd?) #=> [1,2]
48
+ [1,2,3,4].drop_last_if(&:odd?) #=> [1,2,3,4]
49
+ ```
50
+
51
+ `exactly?`
52
+ ------
53
+ Returns if there are exactly the number of an element type.
54
+
55
+ ```ruby
56
+ [].exactly?(1) #=> false
57
+ [1,2,3].exactly?(3) #=> true
58
+ [1,1,3,3].exactly?(2, &:even?) #=> false
59
+ ```
60
+
61
+ `excase?`
62
+ ------
63
+ The same as `exclude?` but tested using `===` instead of `==`.
64
+
65
+ ```ruby
66
+ [1,2,'a'].excase?(String) #=> false
67
+ [1,2,'a'].excase?(3) #=> true
68
+ ```
69
+
70
+ `exclude?`
71
+ ------
72
+ Returns if the collection does not include an object.
73
+
74
+ ```ruby
75
+ [1,2,3].exclude?(4) #=> true
76
+ [1,2,3].exclude?(3) #=> false
77
+ ```
78
+
79
+ `expand`
80
+ ------
81
+ Expand all elements of an Enumerable object.
82
+
83
+ ```ruby
84
+ [0,2..3,5..7].expand #=> [0,[2,3],[5,6,7]]
85
+ ```
86
+
87
+ `exponential`
88
+ ------
89
+ Returns the exponential of a collection of numbers.
90
+
91
+ ```ruby
92
+ [].exponential #=> 0
93
+ [].exponential(nil) #=> nil
94
+ [2,3,4].exponential #=> 4096
95
+ ```
96
+
97
+ `incase?`
98
+ ------
99
+ The same as `include?` but tested using `===` instead of `==`.
100
+
101
+ ```ruby
102
+ [1,2,'a'].incase?(String) #=> true
103
+ [1,2,'a'].incase?(3) #=> false
104
+ ```
105
+
106
+ `interpose`
107
+ ------
108
+ Returns an Enumerator to add seperators.
109
+
110
+ ```ruby
111
+ [1,2,'a'].interpose(:sep).to_a #=> [1,:sep,2,:sep,'a']
112
+ ```
113
+
114
+ `many?`
115
+ ------
116
+ Returns if a collection has more than one element while respecting `nil` and `false` as an element.
117
+
118
+ ```ruby
119
+ [].many? #=> false
120
+ [1,2,3].many? #=> true
121
+ [1,false,nil].many? #=> true
122
+ [1,1,3,3].many?(&:even?) #=> false
123
+
124
+ ```
125
+
126
+ `multiple`
127
+ ------
128
+ Returns the multiplication of a collection of numbers.
129
+
130
+ ```ruby
131
+ [].multiple #=> 0
132
+ [].multiple(nil) #=> nil
133
+ [1,2,3].multiple #=> 6
134
+ ```
135
+
136
+ `occurrences`
137
+ ------
138
+ Returns a hash of the number of times a value appears in an array.
139
+
140
+ ```ruby
141
+ [].occurrences #=> {}
142
+ [1,:symbol,'string',3,:symbol,1].occurrences #=> { 1 => 2,:symbol => 2,'string' => 1,3 => 1 }
143
+ ```
144
+
145
+ `several?`
146
+ ------
147
+ Returns if collection has more than one element while not respecting `nil` and `false` as an element.
148
+
149
+ ```ruby
150
+ [].several? #=> false
151
+ [1,2,3].several? #=> true
152
+ [1,false,nil].several? #=> false
153
+ [1,1,3,3].several?(&:even?) #=> false
154
+ ```
155
+
156
+ `take_last`
157
+ ------
158
+ Returns the last number of elements of a collection.
159
+
160
+ ```ruby
161
+ [].take_last(1) #=> []
162
+ [1,2,3].take_last(1) #=> [3]
163
+ [1,2,3].take_last(2) #=> [2,3]
164
+ ```
165
+
166
+ `take_last_if`
167
+ ------
168
+ Returns the last number of elements of a collection while it meets a criteria.
169
+
170
+ ```ruby
171
+ [].take_last_if(&:odd?) #=> []
172
+ [1,2,3].take_last_if(&:odd?) #=> [3]
173
+ [1,2,3,4].take_last_if(&:odd?) #=> []
174
+ ```