active_object 3.1.0 → 4.0.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/.DS_Store +0 -0
- data/.fasterer.yml +19 -0
- data/.reek +39 -0
- data/.rubocop.yml +38 -0
- data/Gemfile +1 -1
- data/README.md +224 -239
- data/Rakefile +1 -1
- data/active_object.gemspec +19 -16
- data/bin/console +4 -4
- data/bin/rake +6 -7
- data/lib/.DS_Store +0 -0
- data/lib/active_object.rb +4 -33
- data/lib/active_object/.DS_Store +0 -0
- data/lib/active_object/array.rb +55 -41
- data/lib/active_object/date.rb +17 -22
- data/lib/active_object/enumerable.rb +73 -56
- data/lib/active_object/hash.rb +47 -37
- data/lib/active_object/integer.rb +8 -9
- data/lib/active_object/numeric.rb +132 -122
- data/lib/active_object/object.rb +10 -10
- data/lib/active_object/range.rb +1 -1
- data/lib/active_object/settings.rb +17 -0
- data/lib/active_object/string.rb +93 -90
- data/lib/active_object/time.rb +37 -48
- data/lib/active_object/version.rb +1 -1
- data/lib/generators/active_object/install_generator.rb +6 -8
- data/lib/generators/active_object/templates/install.rb +1 -1
- metadata +51 -3
- data/lib/active_object/configuration.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b52b58238d84ad6594aaafcb63c961ac46f6ad81
|
4
|
+
data.tar.gz: 503f279e954d375b381a7b67a6228b3752e11d22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8bad73d734c8812f45f71fb0db9eef0085d561edf6eb4f80c8cd89b8508b2e163b285c2d858042e282e71c70f02d5a10329c62a67a3ecccee19811ef856734b
|
7
|
+
data.tar.gz: 6adf3e1b464d321452602f6a9153163f4270720f829d319cf86f0e197606153125984cce45fc1f520cb22146bd5c0bdf6a58ead42a71f41ef530436b05739b96
|
data/.DS_Store
ADDED
Binary file
|
data/.fasterer.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
speedups:
|
2
|
+
rescue_vs_respond_to: true
|
3
|
+
module_eval: true
|
4
|
+
shuffle_first_vs_sample: true
|
5
|
+
for_loop_vs_each: true
|
6
|
+
each_with_index_vs_while: false
|
7
|
+
map_flatten_vs_flat_map: true
|
8
|
+
reverse_each_vs_reverse_each: true
|
9
|
+
select_first_vs_detect: true
|
10
|
+
sort_vs_sort_by: true
|
11
|
+
fetch_with_argument_vs_block: true
|
12
|
+
keys_each_vs_each_key: true
|
13
|
+
hash_merge_bang_vs_hash_brackets: true
|
14
|
+
block_vs_symbol_to_proc: true
|
15
|
+
proc_call_vs_yield: true
|
16
|
+
gsub_vs_tr: true
|
17
|
+
select_last_vs_reverse_detect: true
|
18
|
+
getter_vs_attr_reader: true
|
19
|
+
setter_vs_attr_writer: true
|
data/.reek
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
---
|
2
|
+
Attribute:
|
3
|
+
enabled: false
|
4
|
+
BooleanParameter:
|
5
|
+
enabled: false
|
6
|
+
ControlParameter:
|
7
|
+
enabled: false
|
8
|
+
DuplicateMethodCall:
|
9
|
+
exclude:
|
10
|
+
- 'ActiveObject::Array#to_sentence'
|
11
|
+
- 'ActiveObject::Array#split'
|
12
|
+
- 'ActiveObject::Array#probability'
|
13
|
+
- 'ActiveObject::Numeric'
|
14
|
+
FeatureEnvy:
|
15
|
+
enabled: false
|
16
|
+
IrresponsibleModule:
|
17
|
+
enabled: false
|
18
|
+
NilCheck:
|
19
|
+
enabled: false
|
20
|
+
ManualDispatch:
|
21
|
+
enabled: false
|
22
|
+
NestedIterators:
|
23
|
+
max_allowed_nesting: 2
|
24
|
+
PrimaDonnaMethod:
|
25
|
+
enabled: false
|
26
|
+
TooManyConstants:
|
27
|
+
exclude:
|
28
|
+
- 'ActiveObject::Date'
|
29
|
+
- 'ActiveObject::Numeric'
|
30
|
+
- 'ActiveObject::Time'
|
31
|
+
TooManyStatements:
|
32
|
+
max_statements: 10
|
33
|
+
exclude:
|
34
|
+
- 'ActiveObject::Array#in_groups'
|
35
|
+
UncommunicativeVariableName:
|
36
|
+
exclude:
|
37
|
+
- 'ActiveObject::Integer#factorial'
|
38
|
+
UtilityFunction:
|
39
|
+
enabled: false
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
DisplayStyleGuide: true
|
4
|
+
TargetRubyVersion: 2.3
|
5
|
+
Exclude:
|
6
|
+
- 'spec/**/**/*'
|
7
|
+
LineLength:
|
8
|
+
Max: 100
|
9
|
+
Metrics/ModuleLength:
|
10
|
+
Enabled: false
|
11
|
+
Style/Alias:
|
12
|
+
EnforcedStyle: prefer_alias_method
|
13
|
+
Style/BracesAroundHashParameters:
|
14
|
+
Enabled: false
|
15
|
+
Style/ClassAndModuleChildren:
|
16
|
+
EnforcedStyle: compact
|
17
|
+
Style/ClosingParenthesisIndentation:
|
18
|
+
Enabled: false
|
19
|
+
Style/Documentation:
|
20
|
+
Enabled: false
|
21
|
+
Style/EmptyLinesAroundBlockBody:
|
22
|
+
Enabled: false
|
23
|
+
Style/EmptyLinesAroundClassBody:
|
24
|
+
Enabled: false
|
25
|
+
Style/EmptyLinesAroundModuleBody:
|
26
|
+
Enabled: false
|
27
|
+
Style/HashSyntax:
|
28
|
+
Enabled: false
|
29
|
+
Style/FirstParameterIndentation:
|
30
|
+
Enabled: false
|
31
|
+
Style/FrozenStringLiteralComment:
|
32
|
+
Enabled: false
|
33
|
+
Style/MultilineMethodCallIndentation:
|
34
|
+
EnforcedStyle: aligned
|
35
|
+
Style/NumericLiterals:
|
36
|
+
Enabled: false
|
37
|
+
Style/RescueModifier:
|
38
|
+
Enabled: false
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -17,7 +17,7 @@ Highly recommended extensions:
|
|
17
17
|
|
18
18
|
Add this line to your application's Gemfile:
|
19
19
|
|
20
|
-
gem
|
20
|
+
gem 'active_object'
|
21
21
|
|
22
22
|
And then execute:
|
23
23
|
|
@@ -48,8 +48,6 @@ Or install it yourself as:
|
|
48
48
|
# config/initalizers/active_object.rb
|
49
49
|
|
50
50
|
ActiveObject.configure do |config|
|
51
|
-
# option = default
|
52
|
-
|
53
51
|
config.autoload_array = true
|
54
52
|
config.autoload_date = true
|
55
53
|
config.autoload_enumerable = true
|
@@ -69,32 +67,32 @@ end
|
|
69
67
|
`after` returns the value after the given value.
|
70
68
|
|
71
69
|
```ruby
|
72
|
-
[
|
73
|
-
[
|
74
|
-
[
|
70
|
+
['1', '2', '3'].after('2') #=> '3'
|
71
|
+
['1', '2', '3'].after('3') #=> '1'
|
72
|
+
['1', '2', '3'].after('4') #=> nil
|
75
73
|
```
|
76
74
|
|
77
75
|
####Before:####
|
78
76
|
`before` returns the value before the given value.
|
79
77
|
|
80
78
|
```ruby
|
81
|
-
[
|
82
|
-
[
|
83
|
-
[
|
79
|
+
['1', '2', '3'].before('2') #=> '1'
|
80
|
+
['1', '2', '3'].before('1') #=> '3'
|
81
|
+
['1', '2', '3'].before('4') #=> nil
|
84
82
|
```
|
85
83
|
|
86
84
|
####Delete First:####
|
87
85
|
`delete_first` and `delete_first!` removes the first element from an array. Like Array.shift, but returns the array instead of the removed element.
|
88
86
|
|
89
87
|
```ruby
|
90
|
-
[
|
88
|
+
['1', '2', '3'].delete_first #=> ['2', '3']
|
91
89
|
```
|
92
90
|
|
93
91
|
####Delete Last:####
|
94
92
|
`delete_last` and `delete_last!` removes the last element from an array. Like Array.pop, but returns the array instead of the removed element.
|
95
93
|
|
96
94
|
```ruby
|
97
|
-
[
|
95
|
+
['1', '2', '3'].delete_last #=> ['1', '2']
|
98
96
|
```
|
99
97
|
|
100
98
|
####Delete Values:####
|
@@ -108,7 +106,7 @@ end
|
|
108
106
|
`dig` returns the value of a nested array.
|
109
107
|
|
110
108
|
```ruby
|
111
|
-
[
|
109
|
+
['zero', ['ten', 'eleven', 'twelve'], 'two'].dig(1, 2) #=> 'twelve'
|
112
110
|
```
|
113
111
|
|
114
112
|
####Duplicates:####
|
@@ -123,34 +121,34 @@ end
|
|
123
121
|
`from` returns the tail of the array from position.
|
124
122
|
|
125
123
|
```ruby
|
126
|
-
[
|
127
|
-
[
|
128
|
-
[
|
124
|
+
['1', '2', '3'].from(0) #=> ['1', '2', '3']
|
125
|
+
['1', '2', '3'].from(1) #=> ['2', '3']
|
126
|
+
['1', '2', '3'].from(-1) #=> ['3']
|
129
127
|
```
|
130
128
|
|
131
129
|
####Groups:####
|
132
130
|
`groups` splits or iterates over the array in number of groups.
|
133
131
|
|
134
132
|
```ruby
|
135
|
-
%w(1 2 3 4 5 6 7 8 9 10).groups(3) #=> [[
|
133
|
+
%w(1 2 3 4 5 6 7 8 9 10).groups(3) #=> [['1', '2', '3', '4'], ['5', '6', '7'], ['8', '9', '10']]
|
136
134
|
```
|
137
135
|
|
138
136
|
####In Groups:####
|
139
137
|
`in_groups` splits or iterates over the array in number of groups, padding any remaining slots with fill_with unless it is false.
|
140
138
|
|
141
139
|
```ruby
|
142
|
-
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3) #=> [[
|
143
|
-
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3, ' ') #=> [[
|
144
|
-
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3, false) #=> [[
|
140
|
+
%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]]
|
141
|
+
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3, ' ') #=> [['1', '2', '3', '4'], ['5', '6', '7', ' '], ['8', '9', '10', ' ']]
|
142
|
+
%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']]
|
145
143
|
```
|
146
144
|
|
147
145
|
####In Groups Of:####
|
148
146
|
`in_groups_of` splits or iterates over the array in groups of size number, padding any remaining slots with fill_with unless it is false.
|
149
147
|
|
150
148
|
```ruby
|
151
|
-
%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3) #=> [[
|
152
|
-
%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, ' ') #=> [[
|
153
|
-
%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, false) #=> [[
|
149
|
+
%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]]
|
150
|
+
%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', ' ', ' ']]
|
151
|
+
%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']]
|
154
152
|
```
|
155
153
|
|
156
154
|
####Percentile:####
|
@@ -195,17 +193,17 @@ end
|
|
195
193
|
`strip` and `strip!` removes blank elements from an array.
|
196
194
|
|
197
195
|
```ruby
|
198
|
-
[
|
199
|
-
|
196
|
+
['this', '', 'that', nil, false].strip #=> ['this', 'that']
|
197
|
+
'this is a test'.split(' ').strip #=> ['this', 'is', 'a', 'test']
|
200
198
|
```
|
201
199
|
|
202
200
|
####To:####
|
203
201
|
`to` returns the beginning of the array up to position.
|
204
202
|
|
205
203
|
```ruby
|
206
|
-
[
|
207
|
-
[
|
208
|
-
[
|
204
|
+
['1', '2', '3'].to(0) #=> ['1']
|
205
|
+
['1', '2', '3'].to(1) #=> ['1', '2']
|
206
|
+
['1', '2', '3'].to(-1) #=> ['3']
|
209
207
|
```
|
210
208
|
|
211
209
|
####To Sentence:####
|
@@ -217,12 +215,12 @@ end
|
|
217
215
|
* last_word_connector: “, and ”
|
218
216
|
|
219
217
|
```ruby
|
220
|
-
[].to_sentence #=>
|
221
|
-
[
|
222
|
-
[
|
223
|
-
[
|
224
|
-
[
|
225
|
-
[
|
218
|
+
[].to_sentence #=> ''
|
219
|
+
['one'].to_sentence #=> 'one'
|
220
|
+
['one', 'two'].to_sentence #=> 'one and two'
|
221
|
+
['one', 'two', 'three'].to_sentence #=> 'one, two, and three'
|
222
|
+
['one', 'two'].to_sentence(two_words_connector: '-') #=> 'one-two'
|
223
|
+
['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ') #=> 'one or two or at least three'
|
226
224
|
```
|
227
225
|
|
228
226
|
## Enumerable
|
@@ -315,8 +313,8 @@ end
|
|
315
313
|
`incase?` the same as #include? but tested using #=== instead of #==.
|
316
314
|
|
317
315
|
```ruby
|
318
|
-
[1, 2,
|
319
|
-
[1, 2,
|
316
|
+
[1, 2, 'a'].incase?(String) #=> true
|
317
|
+
[1, 2, 'a'].incase?(3) #=> false
|
320
318
|
```
|
321
319
|
|
322
320
|
####Many:####
|
@@ -419,9 +417,8 @@ end
|
|
419
417
|
|
420
418
|
```ruby
|
421
419
|
[].sum #=> 0
|
422
|
-
[].sum(nil) #=> nil
|
423
420
|
[1,2,3].sum #=> 6
|
424
|
-
[
|
421
|
+
['foo', 'bar'].sum #=> 'foobar'
|
425
422
|
```
|
426
423
|
|
427
424
|
####Take Last:####
|
@@ -458,8 +455,8 @@ end
|
|
458
455
|
|
459
456
|
```ruby
|
460
457
|
{}.assert_valid_keys(:foo) #=> {}
|
461
|
-
{ foo:
|
462
|
-
{ foo:
|
458
|
+
{ foo: 'bar' }.assert_valid_keys(:foo) #=> { foo: 'bar' }
|
459
|
+
{ foo: 'bar', baz: 'boz' }.assert_valid_keys(:foo, :boo) #=> raises 'ArgumentError: Unknown key: :baz. Valid keys are: :foo, :boo'
|
463
460
|
```
|
464
461
|
|
465
462
|
####Compact:####
|
@@ -468,7 +465,7 @@ end
|
|
468
465
|
```ruby
|
469
466
|
{}.compact #=> {}
|
470
467
|
{ foo: nil }.compact #=> {}
|
471
|
-
{ foo:
|
468
|
+
{ foo: 'bar', baz: false, boo: nil }.compact #=> { foo: 'bar', baz: false }
|
472
469
|
```
|
473
470
|
|
474
471
|
####Deep Merge:####
|
@@ -511,7 +508,7 @@ h1.dig(:a, :b, :c) #=> :d
|
|
511
508
|
`nillify` and `nillify!` transforms all blank values to nil.
|
512
509
|
|
513
510
|
```ruby
|
514
|
-
{ a: 1, b:
|
511
|
+
{ a: 1, b: 'test', c: nil, d: false, e: '', f: ' ' }.nillify #=> {a: 1, b: 'test', c: nil, d: nil, e: nil, f: nil}
|
515
512
|
```
|
516
513
|
|
517
514
|
####Only:####
|
@@ -535,8 +532,8 @@ h1.dig(:a, :b, :c) #=> :d
|
|
535
532
|
`reverse_merge` and `reverse_merge!` merges one hash into other hash.
|
536
533
|
|
537
534
|
```ruby
|
538
|
-
{}.reverse_merge!(foo:
|
539
|
-
{ foo:
|
535
|
+
{}.reverse_merge!(foo: 'bar') #=> { foo: 'bar' }
|
536
|
+
{ foo: 'bar' }.reverse_merge!(baz: 'boo', boo: 'bam') #=> { foo: 'bar', baz: 'boo', boo: 'bam' }
|
540
537
|
```
|
541
538
|
|
542
539
|
####Sample:####
|
@@ -598,7 +595,7 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
598
595
|
`stringify_keys` and `stringify_keys!` converts the hash keys to strings.
|
599
596
|
|
600
597
|
```ruby
|
601
|
-
{ foo:
|
598
|
+
{ foo: 'foo', 'bar' => 'bar' }.stringify_keys #=> { 'foo' => 'foo', 'baz' => 'baz' }
|
602
599
|
```
|
603
600
|
|
604
601
|
####Strip:####
|
@@ -607,35 +604,35 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
607
604
|
```ruby
|
608
605
|
{}.strip #=> {}
|
609
606
|
{ foo: nil, baz: false, boo: '', faz: ' ' }.strip #=> {}
|
610
|
-
{ foo:
|
607
|
+
{ foo: 'bar', baz: false, boo: nil, boz: '', faz: ' ' }.strip #=> { foo: 'bar' }
|
611
608
|
```
|
612
609
|
|
613
610
|
####Symbolize Keys:####
|
614
611
|
`symbolize_keys` and `symbolize_keys!` converts the hash keys to symbols.
|
615
612
|
|
616
613
|
```ruby
|
617
|
-
{ foo:
|
614
|
+
{ foo: 'foo', 'bar' => 'bar' }.symbolize_keys #=> { foo: 'foo', baz: 'baz' }
|
618
615
|
```
|
619
616
|
|
620
617
|
####Symbolize and Underscore Keys:####
|
621
618
|
`symbolize_and_underscore_keys` and `symbolize_and_underscore_keys!` symbolize and underscore hash keys.
|
622
619
|
|
623
620
|
```ruby
|
624
|
-
{
|
621
|
+
{ 'firstName' => 'foo', 'last Name' => 'test' }.symbolize_and_underscore_keys #=> { first_name: 'foo', last_name: 'test' }
|
625
622
|
```
|
626
623
|
|
627
624
|
####Transform Keys:####
|
628
625
|
`transform_keys` and `transform_keys!` a new hash with all keys converted using the block operation.
|
629
626
|
|
630
627
|
```ruby
|
631
|
-
{ foo:
|
628
|
+
{ foo: 'bar', baz: 'boo' }.transform_keys { |k| k.to_s.upcase } #=> { 'FOO' => 'bar', 'BAZ' => 'boo' }
|
632
629
|
```
|
633
630
|
|
634
631
|
####Transform Values:####
|
635
632
|
`transform_values` and `transform_values!` a new hash with all values converted using the block operation.
|
636
633
|
|
637
634
|
```ruby
|
638
|
-
{ foo:
|
635
|
+
{ foo: 'bar', baz: 'boo' }.transform_values { |v| v.to_s.upcase } #=> {foo: 'BAR', baz: 'BOO' }
|
639
636
|
```
|
640
637
|
|
641
638
|
## Integer
|
@@ -651,21 +648,21 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
651
648
|
`of` is like #times but returns a collection of the yield results.
|
652
649
|
|
653
650
|
```ruby
|
654
|
-
3.of { |i|
|
651
|
+
3.of { |i| '#{i+1}' } #=> ['1', '2', '3']
|
655
652
|
```
|
656
653
|
|
657
654
|
####Roman:####
|
658
655
|
`roman` converts this integer to a roman numeral.
|
659
656
|
|
660
657
|
```ruby
|
661
|
-
49.roman #=>
|
658
|
+
49.roman #=> 'XLIX'
|
662
659
|
```
|
663
660
|
|
664
661
|
####Time:####
|
665
662
|
`time` returns a Time object for the given Integer.
|
666
663
|
|
667
664
|
```ruby
|
668
|
-
3.time #=>
|
665
|
+
3.time #=> '1969-12-31 19:00:03.000000000 -0500'
|
669
666
|
```
|
670
667
|
|
671
668
|
## Numeric
|
@@ -988,20 +985,20 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
988
985
|
`ordinal` returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
989
986
|
|
990
987
|
```ruby
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
988
|
+
'1'.ordinal #=> 'th'
|
989
|
+
'2'.ordinal #=> 'nd'
|
990
|
+
'3'.ordinal #=> 'rd'
|
991
|
+
'11'.ordinal #=> 'th'
|
995
992
|
```
|
996
993
|
|
997
994
|
####Ordinalize:####
|
998
995
|
`ordinalize` transforms a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
999
996
|
|
1000
997
|
```ruby
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
998
|
+
'1'.ordinalize #=> '1th'
|
999
|
+
'2'.ordinalize #=> '2nd'
|
1000
|
+
'3'.ordinalize #=> '3rd'
|
1001
|
+
'11'.ordinalize #=> '4th'
|
1005
1002
|
```
|
1006
1003
|
|
1007
1004
|
####Ounces in Ounces:####
|
@@ -1023,9 +1020,9 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
1023
1020
|
`pad` returns a string reprensentation of the number padded with pad_num to a specified length.
|
1024
1021
|
|
1025
1022
|
```ruby
|
1026
|
-
3.pad #=>
|
1027
|
-
3.pad(pad_number: 1) #=>
|
1028
|
-
3.pad(precision: 4) #=>
|
1023
|
+
3.pad #=> '003'
|
1024
|
+
3.pad(pad_number: 1) #=> '113'
|
1025
|
+
3.pad(precision: 4) #=> '0003'
|
1029
1026
|
```
|
1030
1027
|
|
1031
1028
|
####Pad Precision:####
|
@@ -1034,12 +1031,12 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
1034
1031
|
**Options**
|
1035
1032
|
* pad_number: 0
|
1036
1033
|
* precision: 2
|
1037
|
-
* separator:
|
1034
|
+
* separator: '...'
|
1038
1035
|
|
1039
1036
|
```ruby
|
1040
|
-
3.pad_precision #=>
|
1041
|
-
3.5.pad_precision #=>
|
1042
|
-
3.pad_precision(pad_number: 1) #=>
|
1037
|
+
3.pad_precision #=> '3.00'
|
1038
|
+
3.5.pad_precision #=> '3.50'
|
1039
|
+
3.pad_precision(pad_number: 1) #=> '3.11'
|
1043
1040
|
```
|
1044
1041
|
|
1045
1042
|
####Petabytes in Bytes:####
|
@@ -1121,14 +1118,14 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
1121
1118
|
|
1122
1119
|
**Options**
|
1123
1120
|
* precision: 2
|
1124
|
-
* unit:
|
1121
|
+
* unit: '$'
|
1125
1122
|
|
1126
1123
|
```ruby
|
1127
|
-
3.to_currency #=>
|
1128
|
-
3.1.to_currency #=>
|
1129
|
-
3.11.to_currency #=>
|
1130
|
-
3.11111.to_currency #=>
|
1131
|
-
3.to_currency(unit:
|
1124
|
+
3.to_currency #=> '$3.00'
|
1125
|
+
3.1.to_currency #=> '$3.10'
|
1126
|
+
3.11.to_currency #=> '$3.11'
|
1127
|
+
3.11111.to_currency #=> '$3.11'
|
1128
|
+
3.to_currency(unit: '@') #=> '@3.00'
|
1132
1129
|
```
|
1133
1130
|
|
1134
1131
|
####To Length:####
|
@@ -1164,14 +1161,14 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
1164
1161
|
|
1165
1162
|
**Options**
|
1166
1163
|
* precision: 2
|
1167
|
-
* unit:
|
1164
|
+
* unit: '%'
|
1168
1165
|
|
1169
1166
|
```ruby
|
1170
|
-
3.to_percentage #=>
|
1171
|
-
3.1.to_percentage #=>
|
1172
|
-
3.11.to_percentage #=>
|
1173
|
-
3.11111.to_percentage #=>
|
1174
|
-
3.to_percentage(unit:
|
1167
|
+
3.to_percentage #=> '3.00%'
|
1168
|
+
3.1.to_percentage #=> '3.10%'
|
1169
|
+
3.11.to_percentage #=> '3.11%'
|
1170
|
+
3.11111.to_percentage #=> '3.11%'
|
1171
|
+
3.to_percentage(unit: '@') #=> '3.00@'
|
1175
1172
|
```
|
1176
1173
|
|
1177
1174
|
####To Temperature:####
|
@@ -1235,15 +1232,15 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
1235
1232
|
|
1236
1233
|
```ruby
|
1237
1234
|
[].array? #=> true
|
1238
|
-
|
1235
|
+
'Awesome Sting'.array? #=> false
|
1239
1236
|
```
|
1240
1237
|
|
1241
1238
|
####Blank:####
|
1242
1239
|
`blank?` determines if an object is empty or nil.
|
1243
1240
|
|
1244
1241
|
```ruby
|
1245
|
-
|
1246
|
-
|
1242
|
+
''.blank? #=> true
|
1243
|
+
'Awesome Sting'.blank? #=> false
|
1247
1244
|
```
|
1248
1245
|
|
1249
1246
|
####Boolean:####
|
@@ -1252,7 +1249,7 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
|
1252
1249
|
```ruby
|
1253
1250
|
1.boolean? #=> true
|
1254
1251
|
false.boolean? #=> true
|
1255
|
-
|
1252
|
+
'foo'.boolean? #=> false
|
1256
1253
|
```
|
1257
1254
|
|
1258
1255
|
####False:####
|
@@ -1302,24 +1299,24 @@ true.falsey? #=> false
|
|
1302
1299
|
```ruby
|
1303
1300
|
1.numeric? #=> true
|
1304
1301
|
1.0.numeric? #=> true
|
1305
|
-
|
1302
|
+
'1.0'.numeric? #=> false
|
1306
1303
|
```
|
1307
1304
|
|
1308
1305
|
####Numeral:####
|
1309
1306
|
`numeral?` determines if an object's string value is numeral.
|
1310
1307
|
|
1311
1308
|
```ruby
|
1312
|
-
|
1313
|
-
|
1309
|
+
'-32.50'.numeral? #=> true
|
1310
|
+
'$2.55'.numeral? #=> false
|
1314
1311
|
```
|
1315
1312
|
|
1316
1313
|
####Palindrome:####
|
1317
1314
|
`palindrome?` determines if an object is equal when reversed.
|
1318
1315
|
|
1319
1316
|
```ruby
|
1320
|
-
|
1317
|
+
'racecar'.palindrome? #=> true
|
1321
1318
|
12321.palindrome? #=> true
|
1322
|
-
|
1319
|
+
'example'.palindrome? #=> false
|
1323
1320
|
12345.palindrome? #=> false
|
1324
1321
|
```
|
1325
1322
|
|
@@ -1327,8 +1324,8 @@ true.falsey? #=> false
|
|
1327
1324
|
`present?` determines if an object is not empty or nil.
|
1328
1325
|
|
1329
1326
|
```ruby
|
1330
|
-
|
1331
|
-
|
1327
|
+
'Awesome Sting'.present? #=> true
|
1328
|
+
''.present? #=> false
|
1332
1329
|
```
|
1333
1330
|
|
1334
1331
|
####Range:####
|
@@ -1343,8 +1340,8 @@ true.falsey? #=> false
|
|
1343
1340
|
`salvage` returns a placeholder if object is blank?.
|
1344
1341
|
|
1345
1342
|
```ruby
|
1346
|
-
|
1347
|
-
nil.salvage(
|
1343
|
+
' '.salvage #=> '---'
|
1344
|
+
nil.salvage('bar') #=> 'bar'
|
1348
1345
|
123.salvage #=> 123
|
1349
1346
|
```
|
1350
1347
|
|
@@ -1352,7 +1349,7 @@ nil.salvage("bar") #=> "bar"
|
|
1352
1349
|
`string?` determines if an object is a string.
|
1353
1350
|
|
1354
1351
|
```ruby
|
1355
|
-
|
1352
|
+
'foo'.string? #=> true
|
1356
1353
|
1.string? #=> false
|
1357
1354
|
```
|
1358
1355
|
|
@@ -1361,7 +1358,7 @@ nil.salvage("bar") #=> "bar"
|
|
1361
1358
|
|
1362
1359
|
```ruby
|
1363
1360
|
Time.now.time? #=> true
|
1364
|
-
|
1361
|
+
'foo'.time? #=> false
|
1365
1362
|
```
|
1366
1363
|
|
1367
1364
|
####True:####
|
@@ -1385,8 +1382,8 @@ false.truthy? #=> false
|
|
1385
1382
|
`try` and `try!` invokes the public method whose name goes as first argument just like public_send does, except that if the receiver does not respond to it the call returns nil rather than raising an exception.
|
1386
1383
|
|
1387
1384
|
```ruby
|
1388
|
-
|
1389
|
-
|
1385
|
+
'example'.try(:upcase) #=> 'EXAMPLE'
|
1386
|
+
'example'.try(:fake_method) #=> nil
|
1390
1387
|
```
|
1391
1388
|
|
1392
1389
|
## Range
|
@@ -1445,89 +1442,89 @@ false.truthy? #=> false
|
|
1445
1442
|
`any?` determines if a string includes a set of string(s).
|
1446
1443
|
|
1447
1444
|
```ruby
|
1448
|
-
|
1449
|
-
|
1445
|
+
'example string'.any?('foo') #=> false
|
1446
|
+
'example string'.any?('foo', 'string') #=> true
|
1450
1447
|
```
|
1451
1448
|
|
1452
1449
|
####At:####
|
1453
1450
|
`at` returns the characters at index position, matching string, or regex.
|
1454
1451
|
|
1455
1452
|
```ruby
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1453
|
+
'example_string'.at(0) #=> 'e'
|
1454
|
+
'example_string'.at(-2) #=> 'n'
|
1455
|
+
'example_string'.at(1..3) #=> 'xam'
|
1456
|
+
'example_string'.at('e_s') #=> 'e_s'
|
1457
|
+
'example_string'.at(/ple/) #=> 'ple'
|
1458
|
+
'example_string'.at(99) #=> nil
|
1462
1459
|
```
|
1463
1460
|
|
1464
1461
|
####Camelize:####
|
1465
1462
|
`camelize` and `camelize!` transfroms a string to camelcase.
|
1466
1463
|
|
1467
1464
|
```ruby
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1465
|
+
'example_string'.camelize #=> 'ExampleString'
|
1466
|
+
'example_String'.camecase #=> 'ExampleString'
|
1467
|
+
'example_string'.camelize(:lower) #=> 'exampleString'
|
1468
|
+
'example_String'.camecase(:lower) #=> 'exampleString'
|
1472
1469
|
```
|
1473
1470
|
|
1474
1471
|
####Classify:####
|
1475
1472
|
`classify` and `classify!` creates a class name from a string like Rails does for table names to models.
|
1476
1473
|
|
1477
1474
|
```ruby
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1475
|
+
'example_string'.classify #=> 'ExampleString'
|
1476
|
+
'example_string/test'.classify #=> 'ExampleString::Test'
|
1477
|
+
'example_string.test'.classify #=> 'Test'
|
1481
1478
|
```
|
1482
1479
|
|
1483
1480
|
####Constantize:####
|
1484
1481
|
`constantize` converts a string in an object.
|
1485
1482
|
|
1486
1483
|
```ruby
|
1487
|
-
|
1484
|
+
'Example::String'.constantize #=> Class Object
|
1488
1485
|
```
|
1489
1486
|
|
1490
1487
|
####Dasherize:####
|
1491
1488
|
`dasherize` and `dasherize!` replaces underscores with dashes in the string.
|
1492
1489
|
|
1493
1490
|
```ruby
|
1494
|
-
|
1491
|
+
'example_string'.dasherize #=> 'example-string'
|
1495
1492
|
```
|
1496
1493
|
|
1497
1494
|
####Deconstantize:####
|
1498
1495
|
`deconstantize` and `deconstantize!` removes the rightmost segment from the constant expression in the string.
|
1499
1496
|
|
1500
1497
|
```ruby
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1498
|
+
'Example::String'.deconstantize # => 'Example'
|
1499
|
+
'::Example::String'.deconstantize # => '::Example'
|
1500
|
+
'String'.deconstantize # => ''
|
1501
|
+
'::String'.deconstantize # => ''
|
1502
|
+
''.deconstantize # => ''
|
1506
1503
|
```
|
1507
1504
|
|
1508
1505
|
####Demodulize:####
|
1509
1506
|
`demodulize` and `demodulize!` removes the module part from the expression in the string.
|
1510
1507
|
|
1511
1508
|
```ruby
|
1512
|
-
|
1513
|
-
|
1509
|
+
'Example::String'.demodulize #=> 'String'
|
1510
|
+
'String'.demodulize #=> 'String'
|
1514
1511
|
```
|
1515
1512
|
|
1516
1513
|
####Domain:####
|
1517
1514
|
`domain` extracts the domain name from a URL.
|
1518
1515
|
|
1519
1516
|
```ruby
|
1520
|
-
|
1521
|
-
|
1517
|
+
'http://www.example.com/fake-page'.domain #=> 'www.example.com'
|
1518
|
+
'example string'.domain #=> 'example string'
|
1522
1519
|
```
|
1523
1520
|
|
1524
1521
|
####Downcase:####
|
1525
1522
|
`downcase?` returns true if all characters are lowercase.
|
1526
1523
|
|
1527
1524
|
```ruby
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1525
|
+
'example'.downcase? #=> true
|
1526
|
+
'Example'.downcase? #=> false
|
1527
|
+
'EXAMPLE'.downcase? #=> false
|
1531
1528
|
```
|
1532
1529
|
|
1533
1530
|
####Ellipsize:####
|
@@ -1535,47 +1532,47 @@ false.truthy? #=> false
|
|
1535
1532
|
|
1536
1533
|
**Options**
|
1537
1534
|
* offset: 4
|
1538
|
-
* separator:
|
1535
|
+
* separator: '...'
|
1539
1536
|
|
1540
1537
|
```ruby
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1538
|
+
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.ellipsize(30) #=> '0123...WXYZ'
|
1539
|
+
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.ellipsize(50) #=> '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
1540
|
+
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.ellipsize(30, offset: 2) #=> '01...YZ'
|
1541
|
+
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.ellipsize(30, separator: '+++') #=> '0123+++WXYZ'
|
1545
1542
|
```
|
1546
1543
|
|
1547
1544
|
####Exclude:####
|
1548
1545
|
`exclude?` returns true if the string does not include the other string.
|
1549
1546
|
|
1550
1547
|
```ruby
|
1551
|
-
|
1552
|
-
|
1548
|
+
'example_string'.exclude?('exa') #=> false
|
1549
|
+
'example_string'.exclude?('xxx') #=> true
|
1553
1550
|
```
|
1554
1551
|
|
1555
1552
|
####First:####
|
1556
1553
|
`first` returns the first character. If a limit is supplied, returns a substring from the beginning of the string until it reaches the limit value. If the given limit is greater than or equal to the string length, returns a copy of self.
|
1557
1554
|
|
1558
1555
|
```ruby
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1556
|
+
'example'.first #=> 'e'
|
1557
|
+
'example'.first(0) #=> ''
|
1558
|
+
'example'.first(3) #=> 'exa'
|
1562
1559
|
```
|
1563
1560
|
|
1564
1561
|
####Format:####
|
1565
1562
|
`format` returns an interpolated string that allows for options.
|
1566
1563
|
|
1567
1564
|
```ruby
|
1568
|
-
|
1569
|
-
|
1570
|
-
|
1565
|
+
'example %s'.format('string') #=> 'example string'
|
1566
|
+
'test %{one} %{two}'.format(one: 'example', two: 'string') #=> 'test example string'
|
1567
|
+
'%d + %d'.format([1, 2]) #=> '1 + 2'
|
1571
1568
|
```
|
1572
1569
|
|
1573
1570
|
####From:####
|
1574
1571
|
`from` returns a substring from the given position to the end of the string. If the position is negative, it is counted from the end of the string.
|
1575
1572
|
|
1576
1573
|
```ruby
|
1577
|
-
|
1578
|
-
|
1574
|
+
'example'.from(0) #=> 'example'
|
1575
|
+
'example'.from(3) #=> 'mple'
|
1579
1576
|
```
|
1580
1577
|
|
1581
1578
|
####Humanize:####
|
@@ -1585,279 +1582,267 @@ false.truthy? #=> false
|
|
1585
1582
|
* capitalize: true
|
1586
1583
|
|
1587
1584
|
```ruby
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1585
|
+
'ExampleString'.humanize #=> 'Example string'
|
1586
|
+
'_example_string_id'.humanize #=> 'Example string'
|
1587
|
+
'example_string'.humanize(capitalize: false) #=> 'example string'
|
1591
1588
|
```
|
1592
1589
|
|
1593
1590
|
####Indent:####
|
1594
1591
|
`indent` and `indent!` indents the lines in the receiver.
|
1595
1592
|
|
1596
1593
|
```ruby
|
1597
|
-
|
1598
|
-
|
1594
|
+
'example'.indent(2) #=> ' example'
|
1595
|
+
'example'.indent(2, '\t') #=> '\t\texample'
|
1599
1596
|
```
|
1600
1597
|
|
1601
1598
|
####Index all:####
|
1602
1599
|
`index_all` returns the index values of matching patterns.
|
1603
1600
|
|
1604
1601
|
```ruby
|
1605
|
-
|
1606
|
-
|
1607
|
-
|
1602
|
+
'012324507654301243'.index_all(0) #=> [0,7,13]
|
1603
|
+
'the apple is the best fruit in the world'.index_all('the') #=> [0,13,31]
|
1604
|
+
'asdfasdfasdf'.index_all(/sd/) #=> [1,5,9]
|
1608
1605
|
```
|
1609
1606
|
|
1610
1607
|
####Labelize:####
|
1611
1608
|
`labelize` and `labelize!` transforms a string to a human readable string.
|
1612
1609
|
|
1613
1610
|
```ruby
|
1614
|
-
|
1615
|
-
|
1616
|
-
|
1611
|
+
'example string'.labelize #=> 'Example string'
|
1612
|
+
'_example_string_id'.labelize #=> 'Example string ID'
|
1613
|
+
'ExampleString'.labelize #=> 'Example string'
|
1617
1614
|
```
|
1618
1615
|
|
1619
1616
|
####Last:####
|
1620
1617
|
`last` returns the last character of the string. If a limit is supplied, returns a substring from the end of the string until it reaches the limit value (counting backwards). If the given limit is greater than or equal to the string length, returns a copy of self.
|
1621
1618
|
|
1622
1619
|
```ruby
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1620
|
+
'example'.last #=> 'e'
|
1621
|
+
'example'.last(0) #=> ''
|
1622
|
+
'example'.first(3) #=> 'ple'
|
1626
1623
|
```
|
1627
1624
|
|
1628
1625
|
####Mixcase:####
|
1629
1626
|
`mixcase?` returns true if characters are mixedcase.
|
1630
1627
|
|
1631
1628
|
```ruby
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1629
|
+
'Example'.mixedcase? #=> true
|
1630
|
+
'EXAMPLE'.mixedcase? #=> false
|
1631
|
+
'example'.mixedcase? #=> false
|
1635
1632
|
```
|
1636
1633
|
|
1637
1634
|
####Ordinal:####
|
1638
1635
|
`ordinal` returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
1639
1636
|
|
1640
1637
|
```ruby
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1638
|
+
'1'.ordinal #=> 'th'
|
1639
|
+
'2'.ordinal #=> 'nd'
|
1640
|
+
'3'.ordinal #=> 'rd'
|
1641
|
+
'11'.ordinal #=> 'th'
|
1645
1642
|
```
|
1646
1643
|
|
1647
1644
|
####Ordinalize:####
|
1648
1645
|
`ordinalize` transforms a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
1649
1646
|
|
1650
1647
|
```ruby
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1654
|
-
|
1648
|
+
'1'.ordinalize #=> '1th'
|
1649
|
+
'2'.ordinalize #=> '2nd'
|
1650
|
+
'3'.ordinalize #=> '3rd'
|
1651
|
+
'11'.ordinalize #=> '4th'
|
1655
1652
|
```
|
1656
1653
|
|
1657
1654
|
####Parameterize:####
|
1658
1655
|
`parameterize` and `parameterize!` makes string suitable for a dashed url parameter string.
|
1659
1656
|
|
1660
1657
|
```ruby
|
1661
|
-
|
1662
|
-
|
1658
|
+
'example_string'.parameterize #=> 'example-string'
|
1659
|
+
'example_string'.parameterize('?') #=> 'example?string'
|
1663
1660
|
```
|
1664
1661
|
|
1665
1662
|
####Pollute:####
|
1666
1663
|
`pollute` and `pollute!` pollutes the space between every letter in a string, so it will be exempt from any impending string searches.
|
1667
1664
|
|
1668
1665
|
```ruby
|
1669
|
-
|
1670
|
-
|
1666
|
+
'test'.pollute #=> 't^--^--^e^--^--^s^--^--^t^--^--^'
|
1667
|
+
'test'.pollute('-') #=> 't-e-s-t-'
|
1671
1668
|
```
|
1672
1669
|
|
1673
1670
|
####Pop:####
|
1674
1671
|
`pop` returns the last character of a string.
|
1675
1672
|
|
1676
1673
|
```ruby
|
1677
|
-
|
1674
|
+
'test'.pop #=> 't'
|
1678
1675
|
```
|
1679
1676
|
|
1680
1677
|
####Push:####
|
1681
1678
|
`push` concats string to self.
|
1682
1679
|
|
1683
1680
|
```ruby
|
1684
|
-
|
1681
|
+
'test'.push('er') #=> 'tester'
|
1685
1682
|
```
|
1686
1683
|
|
1687
1684
|
####Remove:####
|
1688
1685
|
`remove` and `remove!` removes every instance of a string.
|
1689
1686
|
|
1690
1687
|
```ruby
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1688
|
+
'this thing that thing'.remove('thing') #=> 'this that '
|
1689
|
+
'this thing that thing'.remove(1..3) #=> 't thing that thing'
|
1690
|
+
'this thing that them'.remove('thing', 'them') #=> 'this that '
|
1691
|
+
'this thing that them'.remove('thing', 1..3) #=> 't that them'
|
1695
1692
|
```
|
1696
1693
|
|
1697
1694
|
####Remove Tags:####
|
1698
1695
|
`remove_tags` and `remove_tags!` removes HTML tags from a string.
|
1699
1696
|
|
1700
1697
|
```ruby
|
1701
|
-
|
1702
|
-
|
1703
|
-
|
1698
|
+
'example'.strip_tags #=> 'example'
|
1699
|
+
'<a href='http://example.com'>click</a>'.strip_tags #=> 'click'
|
1700
|
+
'this is <b>bold</b> and <em>emphatic</em>'.strip_tags #=> 'this is bold and emphatic'
|
1704
1701
|
```
|
1705
1702
|
|
1706
1703
|
####Sample:####
|
1707
1704
|
`sample` and `sample!` deletes a random value and returns that value.
|
1708
1705
|
|
1709
1706
|
```ruby
|
1710
|
-
|
1711
|
-
|
1707
|
+
'this thing that'.sample #=> 'thing'
|
1708
|
+
'this thing that'.sample(' thing ') #=> 'that'
|
1712
1709
|
```
|
1713
1710
|
|
1714
1711
|
####Shift:####
|
1715
1712
|
`shift` and `shift!` removes the first instance of a string.
|
1716
1713
|
|
1717
1714
|
```ruby
|
1718
|
-
|
1719
|
-
|
1720
|
-
|
1715
|
+
'this thing that thing'.shift #=> 't'
|
1716
|
+
'this thing that thing'.shift('thing') #=> 'this that thing'
|
1717
|
+
'this thing that thing'.shift('this', 'that') #=> ' thing thing'
|
1721
1718
|
```
|
1722
1719
|
|
1723
1720
|
####Shuffle:####
|
1724
1721
|
`shuffle` and `shuffle!` randomizes the characters in a string.
|
1725
1722
|
|
1726
1723
|
```ruby
|
1727
|
-
|
1728
|
-
|
1724
|
+
'ruby rules'.sample #=> 'e lybrsuur'
|
1725
|
+
'ruby rules'.sample! #=> 'rblse syru'
|
1729
1726
|
```
|
1730
1727
|
|
1731
1728
|
####Sift:####
|
1732
1729
|
`sift` and `sift!` returns a string matching any character in a pattern.
|
1733
1730
|
|
1734
1731
|
```ruby
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1732
|
+
'qa2ws3ed4rf5tg6yh7uj8ik9ol'.sift('0123456789') #=> '23456789'
|
1733
|
+
'qa2ws3ed4rf5tg6yh7uj8ik9ol'.sift(0..9) #=> '23456789'
|
1734
|
+
'qa2ws3ed4rf5tg6yh7uj8ik9ol'.sift([0,1,2,3,4,5,6,7,8,9]) #=> '23456789'
|
1738
1735
|
```
|
1739
1736
|
|
1740
1737
|
####Slugify:####
|
1741
1738
|
`slugify` and `slugify!` generates a permalink-style string, with odd characters removed.
|
1742
1739
|
|
1743
1740
|
```ruby
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1741
|
+
'example'.slugify #=> 'example'
|
1742
|
+
'example string'.slugify #=> 'example-string'
|
1743
|
+
'Example string @@@ test!'.slugify #=> 'example-string-test'
|
1747
1744
|
```
|
1748
1745
|
|
1749
1746
|
####Sort:####
|
1750
1747
|
`sort` and `sort!` sorts a string.
|
1751
1748
|
|
1752
1749
|
```ruby
|
1753
|
-
|
1750
|
+
'adbec'.sort #=> 'abcde'
|
1754
1751
|
```
|
1755
1752
|
|
1756
1753
|
####Squish:####
|
1757
1754
|
`squish` and `squish!` returns the string, first removing all whitespace on both ends of the string, and then changing remaining consecutive whitespace groups into one space each.
|
1758
1755
|
|
1759
1756
|
```ruby
|
1760
|
-
|
1761
|
-
|
1762
|
-
|
1757
|
+
'example string'.squish #=> 'example string'
|
1758
|
+
'example \n \t string'.squish #=> 'example string'
|
1759
|
+
' example string '.squish #=> 'example string'
|
1763
1760
|
```
|
1764
1761
|
|
1765
1762
|
####Titleize:####
|
1766
1763
|
`titleize` and `titleize!` capitalizes each word in a string.
|
1767
1764
|
|
1768
1765
|
```ruby
|
1769
|
-
|
1770
|
-
|
1771
|
-
|
1766
|
+
'example string'.titleize #=> 'Example String'
|
1767
|
+
'_example_string_id'.titleize #=> 'Example String'
|
1768
|
+
'ExampleString'.titleize #=> 'Example String'
|
1772
1769
|
```
|
1773
1770
|
|
1774
1771
|
####To:####
|
1775
1772
|
`to` returns a substring from the beginning of the string to the given position. If the position is negative, it is counted from the end of the string.
|
1776
1773
|
|
1777
1774
|
```ruby
|
1778
|
-
|
1779
|
-
|
1780
|
-
|
1775
|
+
'example'.to(0) #=> 'example'
|
1776
|
+
'example'.to(3) #=> 'exam'
|
1777
|
+
'example'.to(-2) #=> 'exampl'
|
1781
1778
|
```
|
1782
1779
|
|
1783
1780
|
####Truncate:####
|
1784
1781
|
`truncate` a given text after a given length if text is longer than length.
|
1785
1782
|
|
1786
1783
|
**Options**
|
1787
|
-
* omission:
|
1788
|
-
* separator:
|
1784
|
+
* omission: '...'
|
1785
|
+
* separator: ' '
|
1789
1786
|
|
1790
1787
|
```ruby
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1795
|
-
|
1788
|
+
'example string'.truncate(3) #=> '...'
|
1789
|
+
'example string'.truncate(6) #=> 'exa...'
|
1790
|
+
'example string'.truncate(12, separator: ' ') #=> 'example...'
|
1791
|
+
'example string'.truncate(13, omission: '... (more)') #=> 'exa... (more)'
|
1792
|
+
'example string'.truncate(15) #=> 'example string'
|
1796
1793
|
```
|
1797
1794
|
|
1798
1795
|
####Truncate Words:####
|
1799
1796
|
`truncate_words` truncates a given text after a given number of words.
|
1800
1797
|
|
1801
1798
|
**Options**
|
1802
|
-
* omission:
|
1803
|
-
* separator:
|
1799
|
+
* omission: '...'
|
1800
|
+
* separator: ' '
|
1804
1801
|
|
1805
1802
|
```ruby
|
1806
|
-
|
1807
|
-
'Once<br>upon<br>a<br>time<br>in<br>a<br>world'.truncate_words(5, separator: '<br>') #=>
|
1808
|
-
'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)') #=>
|
1803
|
+
'example string test'.truncate_words(1) #=> 'example...'
|
1804
|
+
'Once<br>upon<br>a<br>time<br>in<br>a<br>world'.truncate_words(5, separator: '<br>') #=> 'Once<br>upon<br>a<br>time<br>in...'
|
1805
|
+
'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)') #=> 'And they found that many... (continued)'
|
1809
1806
|
```
|
1810
1807
|
|
1811
1808
|
####Underscore:####
|
1812
1809
|
`underscore` and `underscore!` transforms a string to snakecase.
|
1813
1810
|
|
1814
1811
|
```ruby
|
1815
|
-
|
1816
|
-
|
1817
|
-
|
1812
|
+
'ExampleString'.underscore #=> 'example_string'
|
1813
|
+
'exampleString'.underscore #=> 'example_string'
|
1814
|
+
'ExampleString::Test'.underscore #=> 'example_string/test'
|
1818
1815
|
```
|
1819
1816
|
|
1820
1817
|
####Unpollute:####
|
1821
1818
|
`unpollute` and `unpollute!` removes the default or custom pollution character. Can also be used to remove an unwanted character.
|
1822
1819
|
|
1823
1820
|
```ruby
|
1824
|
-
|
1825
|
-
|
1821
|
+
't^--^--^e^--^--^s^--^--^t^--^--^'.unpollute #=> 'test'
|
1822
|
+
't-e-s-t-'.unpollute #=> 'test'
|
1826
1823
|
```
|
1827
1824
|
|
1828
1825
|
####Upcase:####
|
1829
1826
|
`upcase?` returns true if all characters are uppercase.
|
1830
1827
|
|
1831
1828
|
```ruby
|
1832
|
-
|
1833
|
-
|
1834
|
-
|
1829
|
+
'EXAMPLE'.upcase? #=> true
|
1830
|
+
'example'.upcase? #=> false
|
1831
|
+
'Example'.upcase? #=> false
|
1835
1832
|
```
|
1836
1833
|
|
1837
1834
|
####Unshift:####
|
1838
1835
|
`unshift` and `unshift!` prepends string(s) to self.
|
1839
1836
|
|
1840
1837
|
```ruby
|
1841
|
-
|
1842
|
-
|
1838
|
+
'this thing that thing'.unshift('thing ') #=> 'thing this thing that thing'
|
1839
|
+
'this thing that thing'.unshift('this ', 'that ') #=> 'this that this thing that thing'
|
1843
1840
|
```
|
1844
1841
|
|
1845
1842
|
## Time
|
1846
1843
|
|
1847
1844
|
*Note:* also works with Date class.
|
1848
1845
|
|
1849
|
-
####Count:####
|
1850
|
-
`count_[DURATION]_[TERM]` converts a Date or Time object to format it using a human readable string.
|
1851
|
-
|
1852
|
-
**Durations/Terms**
|
1853
|
-
* seconds, minutes, hours, days, weeks, years, decades, centuries, millenniums
|
1854
|
-
* since, until
|
1855
|
-
|
1856
|
-
```ruby
|
1857
|
-
Date.today.count_hours_since(Date.yesterday) #=> "24.0"
|
1858
|
-
Time.now.count_minutes_until(20.minutes.from_now) #=> "20.0"
|
1859
|
-
```
|
1860
|
-
|
1861
1846
|
####Format:####
|
1862
1847
|
`format` converts a Date or Time object to format it using a human readable string.
|
1863
1848
|
|
@@ -1866,8 +1851,8 @@ Time.now.count_minutes_until(20.minutes.from_now) #=> "20.0"
|
|
1866
1851
|
* Characters can only be used to generate a format part
|
1867
1852
|
|
1868
1853
|
```ruby
|
1869
|
-
Date.today.format(
|
1870
|
-
Time.now.format(
|
1854
|
+
Date.today.format('year') #=> '2014'
|
1855
|
+
Time.now.format('month_name day, year hour:minute ampm') #=> 'January 09, 2014 02:31 pm'
|
1871
1856
|
```
|
1872
1857
|
|
1873
1858
|
| Name | Key | Equivalent `strftime` | Result |
|
@@ -1907,8 +1892,8 @@ Time.now.format("month_name day, year hour:minute ampm") #=> "January 09, 2014 0
|
|
1907
1892
|
**For a full list check out the time extention file.**
|
1908
1893
|
|
1909
1894
|
```ruby
|
1910
|
-
Time.now.to_format(:year) #=>
|
1911
|
-
Time.now.stamp(:datetime) #=>
|
1895
|
+
Time.now.to_format(:year) #=> '2014'
|
1896
|
+
Time.now.stamp(:datetime) #=> 'January 09, 2014 02:31 pm'
|
1912
1897
|
```
|
1913
1898
|
|
1914
1899
|
| Name | Key | Equivalent `strftime` | Result |
|