active_object 1.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 +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +14 -0
- data/.rspec +4 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +1361 -0
- data/Rakefile +1 -0
- data/active_object.gemspec +25 -0
- data/lib/active_object.rb +9 -0
- data/lib/active_object/array.rb +118 -0
- data/lib/active_object/enumerable.rb +172 -0
- data/lib/active_object/hash.rb +203 -0
- data/lib/active_object/integer.rb +7 -0
- data/lib/active_object/numeric.rb +523 -0
- data/lib/active_object/object.rb +41 -0
- data/lib/active_object/string.rb +311 -0
- data/lib/active_object/time.rb +150 -0
- data/lib/active_object/version.rb +3 -0
- data/spec/lib/array_spec.rb +125 -0
- data/spec/lib/enumerable_spec.rb +326 -0
- data/spec/lib/hash_spec.rb +191 -0
- data/spec/lib/integer_spec.rb +11 -0
- data/spec/lib/numeric_spec.rb +604 -0
- data/spec/lib/object_spec.rb +85 -0
- data/spec/lib/string_spec.rb +459 -0
- data/spec/lib/time_spec.rb +455 -0
- data/spec/spec_helper.rb +4 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e96a453245ef6274f0070ee0d457422545066a47
|
4
|
+
data.tar.gz: 68e653784fb298f7aa5a01580b82ca50f3bbd702
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85e2b171390a237569eac7564ae034a71c1d66f09eae85fefcbc82ec2487a0a605f55028216f500f9d501451996a7f050482c4fce65068716e91341a34653881
|
7
|
+
data.tar.gz: 1a137f2100d5cc3fd5e4cf9d821864ec1fd081de1307248737fde159696be859e86be9f1fe227a8ee0337016fa081fa2ba7a7d606b1722d3eed3daeb06325805
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Juan Gomez
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,1361 @@
|
|
1
|
+
# ActiveObject
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/active_object)
|
4
|
+
[](https://travis-ci.org/drexed/active_object)
|
5
|
+
[](https://coveralls.io/r/drexed/active_object)
|
6
|
+
|
7
|
+
ActiveObject is a collection of commonly used object helpers in a ruby based project. It currently includes extensions for: arrays, enumerables, hashes, integers, numerics, objects, strings, and time.
|
8
|
+
|
9
|
+
`Rails Safe` = methods extracted from rails but that do not override that rails method.
|
10
|
+
|
11
|
+
Highly recommended extensions:
|
12
|
+
* **Hash:** Hashie - https://github.com/intridea/hashie
|
13
|
+
* **String:** Escape Utils - https://github.com/brianmario/escape_utils
|
14
|
+
* **String:** Fast Blank - https://github.com/SamSaffron/fast_blank
|
15
|
+
* **Translation:** Fast Gettext - https://github.com/grosser/fast_gettext
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
gem 'active_object'
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install active_object
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### Array
|
34
|
+
|
35
|
+
####Delete First:####
|
36
|
+
`delete_first` removes the first element from an array. Like Array.shift, but returns the array instead of the removed element.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
["1", "2", "3"].delete_first #=> ["2", "3"]
|
40
|
+
```
|
41
|
+
|
42
|
+
####Delete Last:####
|
43
|
+
`delete_last` removes the last element from an array. Like Array.pop, but returns the array instead of the removed element.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
["1", "2", "3"].delete_last #=> ["1", "2"]
|
47
|
+
```
|
48
|
+
|
49
|
+
####From:####
|
50
|
+
`from` returns the tail of the array from position. `Rails Safe`
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
["1", "2", "3"].from(0) #=> ["1", "2", "3"]
|
54
|
+
["1", "2", "3"].from(1) #=> ["2", "3"]
|
55
|
+
["1", "2", "3"].from(-1) #=> ["3"]
|
56
|
+
```
|
57
|
+
|
58
|
+
####Groups:####
|
59
|
+
`groups` splits or iterates over the array in number of groups.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
%w(1 2 3 4 5 6 7 8 9 10).groups(3) #=> [["1", "2", "3", "4"], ["5", "6", "7"], ["8", "9", "10"]]
|
63
|
+
```
|
64
|
+
|
65
|
+
####In Groups:####
|
66
|
+
`in_groups` splits or iterates over the array in number of groups, padding any remaining slots with fill_with unless it is false. `Rails Safe`
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
%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]]
|
70
|
+
%w(1 2 3 4 5 6 7 8 9 10).in_groups(3, ' ') #=> [["1", "2", "3", "4"], ["5", "6", "7", " "], ["8", "9", "10", " "]]
|
71
|
+
%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"]]
|
72
|
+
```
|
73
|
+
|
74
|
+
####In Groups Of:####
|
75
|
+
`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. `Rails Safe`
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
%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]]
|
79
|
+
%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", " ", " "]]
|
80
|
+
%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"]]
|
81
|
+
```
|
82
|
+
|
83
|
+
####Split:####
|
84
|
+
`split` divides the array into one or more subarrays based on a delimiting value or the result of an optional block. `Rails Safe`
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
[1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
|
88
|
+
(1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
|
89
|
+
```
|
90
|
+
|
91
|
+
####Strip:####
|
92
|
+
`strip` removes blank elements from an array.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
["this", "", "that", nil, false].strip #=> ["this", "that"]
|
96
|
+
"this is a test".split(" ").strip #=> ["this", "is", "a", "test"]
|
97
|
+
```
|
98
|
+
|
99
|
+
####To:####
|
100
|
+
`to` returns the beginning of the array up to position. `Rails Safe`
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
["1", "2", "3"].from(0) #=> ["1"]
|
104
|
+
["1", "2", "3"].from(1) #=> ["1", "2"]
|
105
|
+
["1", "2", "3"].from(-1) #=> ["3"]
|
106
|
+
```
|
107
|
+
|
108
|
+
####To Sentence:####
|
109
|
+
`to_sentence` converts the array to a comma-separated sentence where the last element is joined by the connector word. `Rails Safe`
|
110
|
+
|
111
|
+
**Options:**
|
112
|
+
* words_connector: “, ”
|
113
|
+
* two_words_connector: “ and ”
|
114
|
+
* last_word_connector: “, and ”
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
[].to_sentence #=> ""
|
118
|
+
["one"].to_sentence #=> "one"
|
119
|
+
["one", "two"].to_sentence #=> "one and two"
|
120
|
+
["one", "two", "three"].to_sentence #=> "one, two, and three"
|
121
|
+
["one", "two"].to_sentence(two_words_connector: '-') #=> "one-two"
|
122
|
+
["one", "two", "three"].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ') #=> "one or two or at least three"
|
123
|
+
```
|
124
|
+
|
125
|
+
### Enumerable
|
126
|
+
|
127
|
+
####Difference:####
|
128
|
+
`difference` returns the difference of a collection of numbers.
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
[].difference #=> 0
|
132
|
+
[].difference(nil) #=> nil
|
133
|
+
[1,2,3].difference #=> -4
|
134
|
+
```
|
135
|
+
|
136
|
+
####Divisible:####
|
137
|
+
`divisible` returns the division of a collection of numbers.
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
[].divisible #=> 0
|
141
|
+
[].divisible(nil) #=> nil
|
142
|
+
[16,4,2].divisible #=> 2
|
143
|
+
```
|
144
|
+
|
145
|
+
####Drop Last:####
|
146
|
+
`drop_last` drops the last number of elements of a collection.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
[].drop_last(1) #=> []
|
150
|
+
[1,2,3].drop_last(1) #=> [1,2]
|
151
|
+
[1,2,3].drop_last(2) #=> [1]
|
152
|
+
```
|
153
|
+
|
154
|
+
####Drop Last If:####
|
155
|
+
`drop_last_if` drops the last number of elements of a collection while it meets a criteria.
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
[].drop_last_if(&:odd?) #=> []
|
159
|
+
[1,2,3].drop_last_if(&:odd?) #=> [1,2]
|
160
|
+
[1,2,3,4].drop_last_if(&:odd?) #=> [1,2,3,4]
|
161
|
+
```
|
162
|
+
|
163
|
+
####Exactly:####
|
164
|
+
`exactly?` returns if there are exactly the number of an element type.
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
[].exactly?(1) #=> false
|
168
|
+
[1,2,3].excatly?(3) #=> true
|
169
|
+
[1,1,3,3].exactly?(2, &:even?) #=> false
|
170
|
+
```
|
171
|
+
|
172
|
+
####Exclude:####
|
173
|
+
`exclude?` returns true if the collection does not include the object. `Rails Safe`
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
[1, 2, 3].exclude?(4) #=> true
|
177
|
+
[1, 2, 3].exclude?(3) #=> false
|
178
|
+
```
|
179
|
+
|
180
|
+
####Exponential:####
|
181
|
+
`exponential` returns the exponential of a collection of numbers.
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
[].exponential #=> 0
|
185
|
+
[].exponential(nil) #=> nil
|
186
|
+
[2,3,4].exponential #=> 4096
|
187
|
+
```
|
188
|
+
|
189
|
+
####Frequencies:####
|
190
|
+
`frequencies` returns a hash of the number of times a value in an array appears.
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
[].frequencies #=> {}
|
194
|
+
[1, :symbol, 'string', 3, :symbol, 1].frequencies #=> { 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 }
|
195
|
+
```
|
196
|
+
|
197
|
+
####Many:####
|
198
|
+
`many?` returns if collection has more than one element while respecting nil and false as an element. `Rails Safe`
|
199
|
+
|
200
|
+
```ruby
|
201
|
+
[].many? #=> false
|
202
|
+
[1,2,3].many? #=> true
|
203
|
+
[1, false, nil].many? #=> true
|
204
|
+
[1,1,3,3].many?(&:even?) #=> false
|
205
|
+
|
206
|
+
```
|
207
|
+
|
208
|
+
####Max:####
|
209
|
+
`max` returns the largest value of a collection of numbers.
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
[].max #=> 0
|
213
|
+
[].max(nil) #=> nil
|
214
|
+
[1,2,3].max #=> 3
|
215
|
+
```
|
216
|
+
|
217
|
+
####Min:####
|
218
|
+
`min` returns the smallest value of a collection of numbers.
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
[].min #=> 0
|
222
|
+
[].min(nil) #=> nil
|
223
|
+
[1,2,3].min #=> 1
|
224
|
+
```
|
225
|
+
|
226
|
+
####Mean:####
|
227
|
+
`mean` returns the average of a collection of numbers.
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
[].mean #=> 0
|
231
|
+
[].mean(nil) #=> nil
|
232
|
+
[1,2,3].mean #=> 2
|
233
|
+
```
|
234
|
+
|
235
|
+
####Median:####
|
236
|
+
`median` returns the middle value of a collection of numbers.
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
[].median #=> 0
|
240
|
+
[].median(nil) #=> nil
|
241
|
+
[1,2,6].median #=> 2
|
242
|
+
[1,2,3,6].median #=> 2.5
|
243
|
+
```
|
244
|
+
|
245
|
+
####Mode:####
|
246
|
+
`mode` returns the most frequent value of a collection of numbers.
|
247
|
+
|
248
|
+
```ruby
|
249
|
+
[].mode #=> 0
|
250
|
+
[].mode(nil) #=> nil
|
251
|
+
[1,2,3].mode #=> nil
|
252
|
+
[1,1,2,6].mode #=> 1
|
253
|
+
```
|
254
|
+
|
255
|
+
####Multiple:####
|
256
|
+
`multiple` returns the multiplication of a collection of numbers.
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
[].multiple #=> 0
|
260
|
+
[].multiple(nil) #=> nil
|
261
|
+
[1,2,3].multiple #=> 6
|
262
|
+
```
|
263
|
+
|
264
|
+
####Range:####
|
265
|
+
`range` returns the difference between the smallest and largest value of a collection of numbers.
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
[].range #=> 0
|
269
|
+
[].range(nil) #=> nil
|
270
|
+
[1,2,6].range #=> 5
|
271
|
+
```
|
272
|
+
|
273
|
+
####Several:####
|
274
|
+
`several?` returns if collection has more than one element while not respecting nil and false as an element.
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
[].several? #=> false
|
278
|
+
[1,2,3].several? #=> true
|
279
|
+
[1, false, nil].several? #=> false
|
280
|
+
[1,1,3,3].several?(&:even?) #=> false
|
281
|
+
```
|
282
|
+
|
283
|
+
####Standard Deviation:####
|
284
|
+
`standard_deviation` returns the standard deviation of elements of a collection.
|
285
|
+
|
286
|
+
```ruby
|
287
|
+
[].standard_deviation #=> 0
|
288
|
+
[].standard_deviation(nil) #=> nil
|
289
|
+
[1,2,6].standard_deviation #=> 2.6457513110645907
|
290
|
+
```
|
291
|
+
|
292
|
+
####Sum:####
|
293
|
+
`sum` returns the sum of a collection of numbers. `Rails Safe`
|
294
|
+
|
295
|
+
```ruby
|
296
|
+
[].sum #=> 0
|
297
|
+
[].sum(nil) #=> nil
|
298
|
+
[1,2,3].sum #=> 6
|
299
|
+
["foo", "bar"].sum #=> "foobar"
|
300
|
+
```
|
301
|
+
|
302
|
+
####Take Last:####
|
303
|
+
`take_last` returns the last number of elements of a collection.
|
304
|
+
|
305
|
+
```ruby
|
306
|
+
[].take_last(1) #=> []
|
307
|
+
[1,2,3].take_last(1) #=> [3]
|
308
|
+
[1,2,3].take_last(2) #=> [2,3]
|
309
|
+
```
|
310
|
+
|
311
|
+
####Take Last If:####
|
312
|
+
`take_last_if` returns the last number of elements of a collection while it meets a criteria.
|
313
|
+
|
314
|
+
```ruby
|
315
|
+
[].take_last_if(&:odd?) #=> []
|
316
|
+
[1,2,3].take_last_if(&:odd?) #=> [3]
|
317
|
+
[1,2,3,4].take_last_if(&:odd?) #=> []
|
318
|
+
```
|
319
|
+
|
320
|
+
####Variance:####
|
321
|
+
`variance` returns the variance of elements of a collection.
|
322
|
+
|
323
|
+
```ruby
|
324
|
+
[].variance #=> 0
|
325
|
+
[].variance(nil) #=> nil
|
326
|
+
[1,2,6].variance #=> 7
|
327
|
+
```
|
328
|
+
|
329
|
+
### Hash
|
330
|
+
|
331
|
+
####Assert Valid Keys:####
|
332
|
+
`assert_valid_keys` raises an error if key is not included in a list of keys. `Rails Safe`
|
333
|
+
|
334
|
+
```ruby
|
335
|
+
{}.assert_valid_keys(:foo) #=> {}
|
336
|
+
{ foo: "bar" }.assert_valid_keys(:foo) #=> { foo: "bar" }
|
337
|
+
{ foo: "bar", baz: "boz" }.assert_valid_keys(:foo, :boo) #=> raises "ArgumentError: Unknown key: :baz. Valid keys are: :foo, :boo"
|
338
|
+
```
|
339
|
+
|
340
|
+
####Compact:####
|
341
|
+
`compact` and `compact!` returns a hash with non nil values. `Rails Safe`
|
342
|
+
|
343
|
+
```ruby
|
344
|
+
{}.compact #=> {}
|
345
|
+
{ foo: nil }.compact #=> {}
|
346
|
+
{ foo: "bar", baz: false, boo: nil }.compact #=> { foo: "bar", baz: false }
|
347
|
+
```
|
348
|
+
|
349
|
+
####Deep Merge:####
|
350
|
+
`deep_merge` and `deep_merge!` returns a new hash with self and other_hash merged recursively. `Rails Safe`
|
351
|
+
|
352
|
+
```ruby
|
353
|
+
h1 = { a: true, b: { c: [1, 2, 3] } }
|
354
|
+
h2 = { a: false, b: { x: [3, 4, 5] } }
|
355
|
+
|
356
|
+
h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
|
357
|
+
```
|
358
|
+
|
359
|
+
####Except:####
|
360
|
+
`except` and `except!` returns a hash that includes everything but the given keys. `Rails Safe`
|
361
|
+
|
362
|
+
```ruby
|
363
|
+
{}.except(:foo) #=> {}
|
364
|
+
{ foo: 'foo', baz: 'baz', bar: 'bar' }.except(:foo) #=> { baz: 'baz', bar: 'bar' }
|
365
|
+
{ :foo => 'foo', :baz => 'baz', :bar => 'bar' }.except(:baz, :bar) #=> { :foo => 'foo' }
|
366
|
+
```
|
367
|
+
|
368
|
+
####Nillify:####
|
369
|
+
`nillify` and `nillify!` transforms all blank values to nil.
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
{ a: 1, b: "test", c: nil, d: false, e: "", f: " " }.nillify #=> {a: 1, b: 'test', c: nil, d: nil, e: nil, f: nil}
|
373
|
+
```
|
374
|
+
|
375
|
+
####Only:####
|
376
|
+
`only` and `only!` returns only key/value pairs matching certain keys. `Rails Safe`
|
377
|
+
|
378
|
+
```ruby
|
379
|
+
{}.only(:foo) #=> {}
|
380
|
+
{ foo: 'foo', baz: 'baz', bar: 'bar' }.only(:foo) #=> { foo: 'foo' }
|
381
|
+
{ :foo => 'foo', :baz => 'baz', :bar => 'bar' }.only(:baz, :bar) #=> { :baz => 'baz', :bar => 'bar' }
|
382
|
+
```
|
383
|
+
|
384
|
+
####Rename Keys:####
|
385
|
+
`rename_keys` and `rename_keys!` rename the keys of a hash.
|
386
|
+
|
387
|
+
```ruby
|
388
|
+
{ foo: 'foo', baz: 'baz' }.rename_keys(foo: :bar) #=> { bar: 'foo', baz: 'baz' }
|
389
|
+
{ foo: 'foo', 'baz' => 'baz' }.rename_keys(foo: :bar, 'baz' => 'tick') #=> { bar: 'foo', tick: 'baz' }
|
390
|
+
```
|
391
|
+
|
392
|
+
####Reverse Merge:####
|
393
|
+
`reverse_merge` and `reverse_merge!` merges one hash into other hash. `Rails Safe`
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
{}.reverse_merge!(foo: "bar") #=> { foo: "bar" }
|
397
|
+
{ foo: "bar" }.reverse_merge!(baz: "boo", boo: "bam") #=> { foo: "bar", baz: "boo", boo: "bam" }
|
398
|
+
```
|
399
|
+
|
400
|
+
####Slice:####
|
401
|
+
`slice` a hash to include only the given keys. Returns a hash containing the given keys. `Rails Safe`
|
402
|
+
`slice!` replaces the hash with only the given keys. Returns a hash containing the removed key/value pairs. `Rails Safe`
|
403
|
+
|
404
|
+
```ruby
|
405
|
+
h = { a: 1, b: 2, c: 3, d: 4 }
|
406
|
+
|
407
|
+
h.slice(:a, :b) #=> { a: 1, b: 2 }
|
408
|
+
h.slice!(:a, :b) #=> { c: 3, d: 4 }
|
409
|
+
```
|
410
|
+
|
411
|
+
####Stringify Keys:####
|
412
|
+
`stringify_keys` and `stringify_keys!` converts the hash keys to strings. `Rails Safe`
|
413
|
+
|
414
|
+
```ruby
|
415
|
+
{ foo: "foo", "bar" => 'bar' }.stringify_keys #=> { "foo" => "foo", "baz" => "baz" }
|
416
|
+
```
|
417
|
+
|
418
|
+
####Strip:####
|
419
|
+
`strip` and `strip!` returns a hash with non nil, false, or blank values.
|
420
|
+
|
421
|
+
```ruby
|
422
|
+
{}.strip #=> {}
|
423
|
+
{ foo: nil, baz: false, boo: '', faz: ' ' }.strip #=> {}
|
424
|
+
{ foo: "bar", baz: false, boo: nil, boz: '', faz: ' ' }.strip #=> { foo: "bar" }
|
425
|
+
```
|
426
|
+
|
427
|
+
####Symbolize Keys:####
|
428
|
+
`symbolize_keys` and `symbolize_keys!` converts the hash keys to symbols. `Rails Safe`
|
429
|
+
|
430
|
+
```ruby
|
431
|
+
{ foo: "foo", "bar" => "bar" }.symbolize_keys #=> { foo: "foo", baz: "baz" }
|
432
|
+
```
|
433
|
+
|
434
|
+
####Symbolize and Underscore Keys:####
|
435
|
+
`symbolize_and_underscore_keys` and `symbolize_and_underscore_keys!` symbolize and underscore hash keys.
|
436
|
+
|
437
|
+
```ruby
|
438
|
+
{ "firstName" => "foo", "last Name" => "test" }.symbolize_and_underscore_keys #=> { first_name: "foo", last_name: "test" }
|
439
|
+
```
|
440
|
+
|
441
|
+
####Transform Keys:####
|
442
|
+
`transform_keys` and `transform_keys!` a new hash with all keys converted using the block operation. `Rails Safe`
|
443
|
+
|
444
|
+
```ruby
|
445
|
+
{ foo: "bar", baz: "boo" }.transform_keys { |k| k.to_s.upcase } #=> { "FOO" => "bar", "BAZ" => "boo" }
|
446
|
+
```
|
447
|
+
|
448
|
+
####Transform Values:####
|
449
|
+
`transform_values` and `transform_values!` a new hash with all values converted using the block operation. `Rails Safe`
|
450
|
+
|
451
|
+
```ruby
|
452
|
+
{ foo: "bar", baz: "boo" }.transform_values { |v| v.to_s.upcase } #=> {foo: "BAR", baz: "BOO" }
|
453
|
+
```
|
454
|
+
|
455
|
+
### Integer
|
456
|
+
|
457
|
+
####Time:####
|
458
|
+
`time` returns a Time object for the given Integer.
|
459
|
+
|
460
|
+
```ruby
|
461
|
+
3.time #=> "1969-12-31 19:00:03.000000000 -0500"
|
462
|
+
```
|
463
|
+
|
464
|
+
### Numeric
|
465
|
+
|
466
|
+
####Add:####
|
467
|
+
`add` returns the sum of two numbers.
|
468
|
+
|
469
|
+
```ruby
|
470
|
+
4.add(2) #=> 6
|
471
|
+
```
|
472
|
+
|
473
|
+
####Bytes:####
|
474
|
+
`byte` and `bytes` returns self.
|
475
|
+
|
476
|
+
```ruby
|
477
|
+
3.bytes #=> 3
|
478
|
+
```
|
479
|
+
|
480
|
+
####Centigrams:####
|
481
|
+
`centigram` and `centigrams` returns the amount of grams in n centigrams.
|
482
|
+
|
483
|
+
```ruby
|
484
|
+
3.centigrams #=> 0.03
|
485
|
+
```
|
486
|
+
|
487
|
+
####Centimeters:####
|
488
|
+
`centimeter` and `centimeters` returns the amount of meters in n centimeters.
|
489
|
+
|
490
|
+
```ruby
|
491
|
+
3.centimeters #=> 0.03
|
492
|
+
```
|
493
|
+
|
494
|
+
####Centuries:####
|
495
|
+
`century` and `centuries` returns the amount of seconds in n centuries.
|
496
|
+
|
497
|
+
```ruby
|
498
|
+
3.centuries #=> 9467280000.0
|
499
|
+
```
|
500
|
+
|
501
|
+
####Decades:####
|
502
|
+
`decade` and `decades` returns the amount of seconds in n decades.
|
503
|
+
|
504
|
+
```ruby
|
505
|
+
3.decades #=> 946728000.0
|
506
|
+
```
|
507
|
+
####Decagrams:####
|
508
|
+
`decagram` and `decagrams` returns the amount of grams in n decagrams.
|
509
|
+
|
510
|
+
```ruby
|
511
|
+
3.decagrams #=> 30
|
512
|
+
```
|
513
|
+
|
514
|
+
####Decameters:####
|
515
|
+
`decameter` and `decameters` returns the amount of meters in n decameters.
|
516
|
+
|
517
|
+
```ruby
|
518
|
+
3.decameters #=> 30
|
519
|
+
```
|
520
|
+
|
521
|
+
####Decigrams:####
|
522
|
+
`decigram` and `decigrams` returns the amount of grams in n decigrams.
|
523
|
+
|
524
|
+
```ruby
|
525
|
+
3.decigrams #=> 0.3
|
526
|
+
```
|
527
|
+
|
528
|
+
####Decimeters:####
|
529
|
+
`decimeter` and `decimeters` returns the amount of meters in n decimeters.
|
530
|
+
|
531
|
+
```ruby
|
532
|
+
3.decimeters #=> 0.3
|
533
|
+
```
|
534
|
+
|
535
|
+
####Days:####
|
536
|
+
`day` and `days` returns the amount of seconds in n days.
|
537
|
+
|
538
|
+
```ruby
|
539
|
+
3.days #=> 259200
|
540
|
+
```
|
541
|
+
|
542
|
+
####Divide:####
|
543
|
+
`divide` returns the division of two numbers.
|
544
|
+
|
545
|
+
```ruby
|
546
|
+
4.divide(2) #=> 2
|
547
|
+
```
|
548
|
+
|
549
|
+
####Exabytes:####
|
550
|
+
`exabyte` and `exabytes` returns the amount of bytes in n exabytes.
|
551
|
+
|
552
|
+
```ruby
|
553
|
+
3.exabytes #=> 3458764513820540928
|
554
|
+
```
|
555
|
+
|
556
|
+
####Feet:####
|
557
|
+
`foot` and `feet` returns the amount of inches in n feet.
|
558
|
+
|
559
|
+
```ruby
|
560
|
+
3.feet #=> 36
|
561
|
+
```
|
562
|
+
|
563
|
+
####Gigabytes:####
|
564
|
+
`gigabyte` and `gigabytes` returns the amount of bytes in n gigabytes.
|
565
|
+
|
566
|
+
```ruby
|
567
|
+
3.gigabytes #=> 3221225472
|
568
|
+
```
|
569
|
+
|
570
|
+
####Grams:####
|
571
|
+
`gram` and `grams` returns self.
|
572
|
+
|
573
|
+
```ruby
|
574
|
+
3.grams #=> 3
|
575
|
+
```
|
576
|
+
|
577
|
+
####Hectograms:####
|
578
|
+
`hectogram` and `hectograms` returns the amount of grams in n hectograms.
|
579
|
+
|
580
|
+
```ruby
|
581
|
+
3.hectograms #=> 300
|
582
|
+
```
|
583
|
+
|
584
|
+
####Hectometers:####
|
585
|
+
`hectometer` and `hectometers` returns the amount of meters in n hectometers.
|
586
|
+
|
587
|
+
```ruby
|
588
|
+
3.hectometers #=> 300
|
589
|
+
```
|
590
|
+
|
591
|
+
####Hours:####
|
592
|
+
`hour` and `hours` returns the amount of seconds in n hours.
|
593
|
+
|
594
|
+
```ruby
|
595
|
+
3.hours #=> 10800
|
596
|
+
```
|
597
|
+
|
598
|
+
####Inches:####
|
599
|
+
`inch` and `inches` returns the amount of inches in n inches.
|
600
|
+
|
601
|
+
```ruby
|
602
|
+
3.inches #=> 3
|
603
|
+
```
|
604
|
+
|
605
|
+
####Kilobytes:####
|
606
|
+
`kilobyte` and `kilobytes` returns the amount of bytes in n kilobytes.
|
607
|
+
|
608
|
+
```ruby
|
609
|
+
3.kilobytes #=> 3072
|
610
|
+
```
|
611
|
+
|
612
|
+
####Kilograms:####
|
613
|
+
`kilogram` and `kilograms` returns the amount of grams in n kilograms.
|
614
|
+
|
615
|
+
```ruby
|
616
|
+
3.kilograms #=> 3000
|
617
|
+
```
|
618
|
+
|
619
|
+
####Kilometers:####
|
620
|
+
`kilometer` and `kilometers` returns the amount of meters in n kilometers.
|
621
|
+
|
622
|
+
```ruby
|
623
|
+
3.kilometers #=> 3000
|
624
|
+
```
|
625
|
+
|
626
|
+
####Metric Ton:####
|
627
|
+
`metric_ton` and `metric_tons` returns the amount of grams in n metric_tons.
|
628
|
+
|
629
|
+
```ruby
|
630
|
+
3.metric_tons #=> 3000000
|
631
|
+
```
|
632
|
+
|
633
|
+
####Megabytes:####
|
634
|
+
`megabyte` and `megabytes` returns the amount of bytes in n megabytes.
|
635
|
+
|
636
|
+
```ruby
|
637
|
+
3.megabytes #=> 3145728
|
638
|
+
```
|
639
|
+
|
640
|
+
####Meters:####
|
641
|
+
`meter` and `meters` returns self.
|
642
|
+
|
643
|
+
```ruby
|
644
|
+
3.meters #=> 3
|
645
|
+
```
|
646
|
+
|
647
|
+
####Miles:####
|
648
|
+
`mile` and `miles` returns the amount of inches in n miles.
|
649
|
+
|
650
|
+
```ruby
|
651
|
+
3.miles #=> 190080
|
652
|
+
```
|
653
|
+
|
654
|
+
####Millennium:####
|
655
|
+
`millennium` and `millenniums` returns the amount of seconds in n millenniums.
|
656
|
+
|
657
|
+
```ruby
|
658
|
+
3.millenniums #=> 94672800000.0
|
659
|
+
```
|
660
|
+
|
661
|
+
####Milligram:####
|
662
|
+
`milligram` and `milligrams` returns the amount of grams in n milligrams.
|
663
|
+
|
664
|
+
```ruby
|
665
|
+
3.milligrams #=> 0.003
|
666
|
+
```
|
667
|
+
|
668
|
+
####Millimeters:####
|
669
|
+
`millimeter` and `millimeters` returns the amount of meters in n millimeters.
|
670
|
+
|
671
|
+
```ruby
|
672
|
+
3.millimeters #=> 0.003
|
673
|
+
```
|
674
|
+
|
675
|
+
####Minutes:####
|
676
|
+
`minute` and `minutes` returns the amount of seconds in n minutes.
|
677
|
+
|
678
|
+
```ruby
|
679
|
+
3.minutes #=> 180
|
680
|
+
```
|
681
|
+
|
682
|
+
####Multiply:####
|
683
|
+
`multiply` returns the multiplication of two numbers.
|
684
|
+
|
685
|
+
```ruby
|
686
|
+
4.multiply(2) #=> 8
|
687
|
+
```
|
688
|
+
|
689
|
+
####Multiple Of:####
|
690
|
+
`multiple_of?` returns true if a number can be evenly divided by n. `Rails Safe`
|
691
|
+
|
692
|
+
```ruby
|
693
|
+
9.multiple_of?(3) #=> true
|
694
|
+
7.multiple_of?(3) #=> false
|
695
|
+
```
|
696
|
+
|
697
|
+
####Nautical Miles:####
|
698
|
+
`nautical_mile` and `nautical_miles` returns the amount of inches in n nautical miles.
|
699
|
+
|
700
|
+
```ruby
|
701
|
+
3.nautical_miles #=> 218740.26239999998
|
702
|
+
```
|
703
|
+
|
704
|
+
####Negative:####
|
705
|
+
`negative?` returns true if a number is less than zero.
|
706
|
+
|
707
|
+
```ruby
|
708
|
+
-1.negative? #=> true
|
709
|
+
1.negative? #=> false
|
710
|
+
```
|
711
|
+
|
712
|
+
####Ordinal:####
|
713
|
+
`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. `Rails Safe`
|
714
|
+
|
715
|
+
```ruby
|
716
|
+
"1".ordinal #=> "th"
|
717
|
+
"2".ordinal #=> "nd"
|
718
|
+
"3".ordinal #=> "rd"
|
719
|
+
"11".ordinal #=> "th"
|
720
|
+
```
|
721
|
+
|
722
|
+
####Ordinalize:####
|
723
|
+
`ordinalize` transforms a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. `Rails Safe`
|
724
|
+
|
725
|
+
```ruby
|
726
|
+
"1".ordinalize #=> "1th"
|
727
|
+
"2".ordinalize #=> "2nd"
|
728
|
+
"3".ordinalize #=> "3rd"
|
729
|
+
"11".ordinalize #=> "4th"
|
730
|
+
```
|
731
|
+
|
732
|
+
####Ounces:####
|
733
|
+
`ounce` and `ounces` returns self.
|
734
|
+
|
735
|
+
```ruby
|
736
|
+
3.ounces #=> 48
|
737
|
+
```
|
738
|
+
|
739
|
+
####Pad:####
|
740
|
+
`pad` returns a string reprensentation of the number padded with pad_num to a specified length.
|
741
|
+
|
742
|
+
```ruby
|
743
|
+
3.pad #=> "003"
|
744
|
+
3.pad(pad_number: 1) #=> "113"
|
745
|
+
3.pad(precision: 4) #=> "0003"
|
746
|
+
```
|
747
|
+
|
748
|
+
####Pad Precision:####
|
749
|
+
`pad_precision` returns a string of padded after the '.' to n amount.
|
750
|
+
|
751
|
+
**Options**
|
752
|
+
* pad_number: 0
|
753
|
+
* precision: 2
|
754
|
+
* separator: "..."
|
755
|
+
|
756
|
+
```ruby
|
757
|
+
3.pad_precision #=> "3.00"
|
758
|
+
3.5.pad_precision #=> "3.50"
|
759
|
+
3.pad_precision(pad_number: 1) #=> "3.11"
|
760
|
+
```
|
761
|
+
|
762
|
+
####Petabytes:####
|
763
|
+
`petabyte` and `pegabytes` returns the amount of bytes in n petabytes.
|
764
|
+
|
765
|
+
```ruby
|
766
|
+
3.petabytes #=> 3377699720527872
|
767
|
+
```
|
768
|
+
|
769
|
+
####Positive:####
|
770
|
+
`positive?` returns true if a number is greater than zero.
|
771
|
+
|
772
|
+
```ruby
|
773
|
+
1.positive? #=> true
|
774
|
+
-1.positive? #=> false
|
775
|
+
```
|
776
|
+
|
777
|
+
####Pounds:####
|
778
|
+
`pounds` and `pounds` returns the amount of ounces in n pounds.
|
779
|
+
|
780
|
+
```ruby
|
781
|
+
3.pounds #=> 48
|
782
|
+
```
|
783
|
+
|
784
|
+
####Power:####
|
785
|
+
`power` returns the nth power of a number.
|
786
|
+
|
787
|
+
```ruby
|
788
|
+
4.power(2) #=> 16
|
789
|
+
```
|
790
|
+
|
791
|
+
####Root:####
|
792
|
+
`root` returns the nth root of a number.
|
793
|
+
|
794
|
+
```ruby
|
795
|
+
4.root(2) #=> 2
|
796
|
+
```
|
797
|
+
|
798
|
+
####Seconds:####
|
799
|
+
`second` and `seconds` returns self.
|
800
|
+
|
801
|
+
```ruby
|
802
|
+
3.seconds #=> 3
|
803
|
+
```
|
804
|
+
|
805
|
+
####Stones:####
|
806
|
+
`stone` and `stone` returns the amount of ounces in n stones.
|
807
|
+
|
808
|
+
```ruby
|
809
|
+
3.stones #=> 672
|
810
|
+
```
|
811
|
+
|
812
|
+
####Subtract:####
|
813
|
+
`subtract` returns the difference of two numbers.
|
814
|
+
|
815
|
+
```ruby
|
816
|
+
4.subtract(2) #=> 2
|
817
|
+
```
|
818
|
+
|
819
|
+
####Terabytes:####
|
820
|
+
`terabyte` and `terabytes` returns the amount of bytes in n terabytes.
|
821
|
+
|
822
|
+
```ruby
|
823
|
+
3.terabytes #=> 3298534883328
|
824
|
+
```
|
825
|
+
|
826
|
+
####To Byte:####
|
827
|
+
`to_byte` converts a byte size from one unit to another unit.
|
828
|
+
|
829
|
+
```ruby
|
830
|
+
1.to_byte(:byte, :byte) #=> 1 #B
|
831
|
+
5120.to_byte(:byte, :kilobyte) #=> 5 #MB
|
832
|
+
1.to_byte(:megabyte, :kilobyte) #=> 1024 #KB
|
833
|
+
80.to_byte(:megabyte, :gigabyte) #=> 0.078125 #GB
|
834
|
+
```
|
835
|
+
|
836
|
+
####To Length:####
|
837
|
+
`to_length` converts a length from one unit to another unit.
|
838
|
+
|
839
|
+
```ruby
|
840
|
+
12.to_length(:inches, :feet) #=> 12 #IN
|
841
|
+
3000.to_length(:meters, :kilometers) #=> 3 #KM
|
842
|
+
1.to_length(:feet, :centimeters) #=> 30.479999999999997 #CM
|
843
|
+
1.to_length(:kilometer, :yards) #=> 1093.6138888888888 #YDS
|
844
|
+
```
|
845
|
+
|
846
|
+
####To Mass:####
|
847
|
+
`to_mass` converts a mass from one unit to another unit.
|
848
|
+
|
849
|
+
```ruby
|
850
|
+
16.to_mass(:ounces, :pounds) #=> 1 #LB
|
851
|
+
1.to_mass(:centigrams, :milligrams) #=> 10 #MG
|
852
|
+
3.to_mass(:pound, :kilogram) #=> 1.360776 #KG
|
853
|
+
1.to_mass(:kilograms, :pounds) #=> 2.204625 #LB
|
854
|
+
```
|
855
|
+
|
856
|
+
####To Nearest Value:####
|
857
|
+
`to_nearest value` return the value in values that is nearest to the number.
|
858
|
+
|
859
|
+
```ruby
|
860
|
+
5.to_nearest_value([1, 3, 6, 9]) #=> 6
|
861
|
+
3.5.to_nearest_value([3.0, 3.3, 3.6, 3.9]) #=> 3.6
|
862
|
+
```
|
863
|
+
|
864
|
+
####To Temperature:####
|
865
|
+
`to_temperature` converts a temperature from one unit to another unit.
|
866
|
+
|
867
|
+
```ruby
|
868
|
+
100.to_temperature(:celsius, :fahrenheit) #=> 212 #F
|
869
|
+
212.to_temperature(:fahrenheit, :celsius) #=> 100 #C
|
870
|
+
212.to_temperature(:fahrenheit, :kelvin) #=> 373.15 #K
|
871
|
+
```
|
872
|
+
|
873
|
+
####To Time:####
|
874
|
+
`to_time` converts a time unit from one unit to another unit.
|
875
|
+
|
876
|
+
```ruby
|
877
|
+
120.to_time(:seconds, :mintues) #=> 2 #MIN
|
878
|
+
3.to_time(:hours, :days) #=> 3 #DAY
|
879
|
+
2.to_time(:days, :seconds) #=> 172800 #SEC
|
880
|
+
1825.to_time(:days, :years) #=> 4.996577686516085 #YR
|
881
|
+
```
|
882
|
+
|
883
|
+
####Tons:####
|
884
|
+
`ton` and `ton` returns the amount of ounces in n tons.
|
885
|
+
|
886
|
+
```ruby
|
887
|
+
3.tons #=> 96000
|
888
|
+
```
|
889
|
+
|
890
|
+
####Weeks:####
|
891
|
+
`week` and `weeks` returns the amount of seconds in n weeks.
|
892
|
+
|
893
|
+
```ruby
|
894
|
+
3.weeks #=> 1814400
|
895
|
+
```
|
896
|
+
|
897
|
+
####Yards:####
|
898
|
+
`yard` and `yards` returns the amount of inches in n yards.
|
899
|
+
|
900
|
+
```ruby
|
901
|
+
3.yards #=> 108
|
902
|
+
```
|
903
|
+
|
904
|
+
####Years:####
|
905
|
+
`year` and `years` returns the amount of seconds in n years.
|
906
|
+
|
907
|
+
```ruby
|
908
|
+
3.years #=> 94672800.0
|
909
|
+
```
|
910
|
+
|
911
|
+
### Object
|
912
|
+
|
913
|
+
####Blank:####
|
914
|
+
`blank?` determines if an object is empty or nil. `Rails Safe`
|
915
|
+
|
916
|
+
```ruby
|
917
|
+
"".blank? #=> true
|
918
|
+
"Awesome Sting".blank? #=> false
|
919
|
+
```
|
920
|
+
|
921
|
+
####Numeric:####
|
922
|
+
`numeric?` determines if an object's string value is numeric.
|
923
|
+
|
924
|
+
```ruby
|
925
|
+
"-32.50".numeric? #=> true
|
926
|
+
"$2.55".numeric? #=> false
|
927
|
+
```
|
928
|
+
|
929
|
+
####Palindrome:####
|
930
|
+
`palindrome?` determines if an object is equal when reversed.
|
931
|
+
|
932
|
+
```ruby
|
933
|
+
"racecar".palindrome? #=> true
|
934
|
+
12321.palindrome? #=> true
|
935
|
+
"example".palindrome? #=> false
|
936
|
+
12345.palindrome? #=> false
|
937
|
+
```
|
938
|
+
|
939
|
+
####Present:####
|
940
|
+
`present?` determines if an object is not empty or nil. `Rails Safe`
|
941
|
+
|
942
|
+
```ruby
|
943
|
+
"Awesome Sting".blank? #=> true
|
944
|
+
"".present? #=> false
|
945
|
+
```
|
946
|
+
|
947
|
+
####Try:####
|
948
|
+
`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. `Rails Safe`
|
949
|
+
|
950
|
+
```ruby
|
951
|
+
"example".try(:upcase) #=> "EXAMPLE"
|
952
|
+
"example".try(:fake_method) #=> nil
|
953
|
+
```
|
954
|
+
|
955
|
+
### String
|
956
|
+
|
957
|
+
####At:####
|
958
|
+
`at` returns the characters at index position, matching string, or regex. `Rails Safe`
|
959
|
+
|
960
|
+
```ruby
|
961
|
+
"example_string".at(0) #=> "e"
|
962
|
+
"example_string".at(-2) #=> "n"
|
963
|
+
"example_string".at(1..3) #=> "xam"
|
964
|
+
"example_string".at("e_s") #=> "e_s"
|
965
|
+
"example_string".at(/ple/) #=> "ple"
|
966
|
+
"example_string".at(99) #=> nil
|
967
|
+
```
|
968
|
+
|
969
|
+
####Camelize:####
|
970
|
+
`camelize` and `camelize!` transfroms a string to camelcase. `Rails Safe`
|
971
|
+
|
972
|
+
```ruby
|
973
|
+
"example_string".camelize #=> "ExampleString"
|
974
|
+
"example_String".camecase #=> "ExampleString"
|
975
|
+
"example_string".camelize(:lower) #=> "exampleString"
|
976
|
+
"example_String".camecase(:lower) #=> "exampleString"
|
977
|
+
```
|
978
|
+
|
979
|
+
####Classify:####
|
980
|
+
`classify` and `classify!` creates a class name from a string like Rails does for table names to models. `Rails Safe`
|
981
|
+
|
982
|
+
```ruby
|
983
|
+
"example_string".classify #=> "ExampleString"
|
984
|
+
"example_string/test".classify #=> "ExampleString::Test"
|
985
|
+
"example_string.test".classify #=> "Test"
|
986
|
+
```
|
987
|
+
|
988
|
+
####Dasherize:####
|
989
|
+
`dasherize` and `dasherize!` replaces underscores with dashes in the string. `Rails Safe`
|
990
|
+
|
991
|
+
```ruby
|
992
|
+
"example_string".dasherize #=> "example-string"
|
993
|
+
```
|
994
|
+
|
995
|
+
####Demodulize:####
|
996
|
+
`demodulize` and `demodulize!` removes the module part from the expression in the string. `Rails Safe`
|
997
|
+
|
998
|
+
```ruby
|
999
|
+
"Example::String".demodulize #=> "String"
|
1000
|
+
"String".demodulize #=> "String"
|
1001
|
+
```
|
1002
|
+
|
1003
|
+
####Domain:####
|
1004
|
+
`domain` extracts the domain name from a URL.
|
1005
|
+
|
1006
|
+
```ruby
|
1007
|
+
"http://www.example.com/fake-page".domain #=> "www.example.com"
|
1008
|
+
"example string".domain #=> "example string"
|
1009
|
+
```
|
1010
|
+
|
1011
|
+
####Downcase:####
|
1012
|
+
`downcase?` returns true if all characters are lowercase.
|
1013
|
+
|
1014
|
+
```ruby
|
1015
|
+
"example".downcase? #=> true
|
1016
|
+
"Example".downcase? #=> false
|
1017
|
+
"EXAMPLE".downcase? #=> false
|
1018
|
+
```
|
1019
|
+
|
1020
|
+
####Ellipsize:####
|
1021
|
+
`ellipsize` truncate a string in the middle.
|
1022
|
+
|
1023
|
+
**Options**
|
1024
|
+
* offset: 4
|
1025
|
+
* separator: "..."
|
1026
|
+
|
1027
|
+
```ruby
|
1028
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize(30) #=> "0123...WXYZ"
|
1029
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize(50) #=> "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
1030
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize(30, offset: 2) #=> "01...YZ"
|
1031
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ellipsize(30, separator: "+++") #=> "0123+++WXYZ"
|
1032
|
+
```
|
1033
|
+
|
1034
|
+
####Exclude:####
|
1035
|
+
`exclude?` returns true if the string does not include the other string. `Rails Safe`
|
1036
|
+
|
1037
|
+
```ruby
|
1038
|
+
"example_string".exclude?("exa") #=> false
|
1039
|
+
"example_string".exclude?("xxx") #=> true
|
1040
|
+
```
|
1041
|
+
|
1042
|
+
####First:####
|
1043
|
+
`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. `Rails Safe`
|
1044
|
+
|
1045
|
+
```ruby
|
1046
|
+
"example".first #=> "e"
|
1047
|
+
"example".first(0) #=> ""
|
1048
|
+
"example".first(3) #=> "exa"
|
1049
|
+
```
|
1050
|
+
|
1051
|
+
####From:####
|
1052
|
+
`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. `Rails Safe`
|
1053
|
+
|
1054
|
+
```ruby
|
1055
|
+
"example".from(0) #=> "example"
|
1056
|
+
"example".from(3) #=> "mple"
|
1057
|
+
```
|
1058
|
+
|
1059
|
+
####Humanize:####
|
1060
|
+
`humanize` and `humanize!` transforms a string to a human readable string. `Rails Safe`
|
1061
|
+
|
1062
|
+
**Options**
|
1063
|
+
* capitalize: true
|
1064
|
+
|
1065
|
+
```ruby
|
1066
|
+
"ExampleString".humanize #=> "Example string"
|
1067
|
+
"example_string".humanize #=> "Example string"
|
1068
|
+
"example_string".humanize(capitalize: false) #=> "example string"
|
1069
|
+
```
|
1070
|
+
|
1071
|
+
####Indent:####
|
1072
|
+
`indent` and `indent!` indents the lines in the receiver. `Rails Safe`
|
1073
|
+
|
1074
|
+
```ruby
|
1075
|
+
"example".indent(2) #=> " example"
|
1076
|
+
"example".indent(2, "\t") #=> "\t\texample"
|
1077
|
+
```
|
1078
|
+
|
1079
|
+
####Last:####
|
1080
|
+
`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. `Rails Safe`
|
1081
|
+
|
1082
|
+
```ruby
|
1083
|
+
"example".last #=> "e"
|
1084
|
+
"example".last(0) #=> ""
|
1085
|
+
"example".first(3) #=> "ple"
|
1086
|
+
```
|
1087
|
+
|
1088
|
+
####Mixcase:####
|
1089
|
+
`mixcase?` returns true if characters are mixedcase.
|
1090
|
+
|
1091
|
+
```ruby
|
1092
|
+
"Example".mixedcase? #=> true
|
1093
|
+
"EXAMPLE".mixedcase? #=> false
|
1094
|
+
"example".mixedcase? #=> false
|
1095
|
+
```
|
1096
|
+
|
1097
|
+
####Ordinal:####
|
1098
|
+
`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. `Rails Safe`
|
1099
|
+
|
1100
|
+
```ruby
|
1101
|
+
"1".ordinal #=> "th"
|
1102
|
+
"2".ordinal #=> "nd"
|
1103
|
+
"3".ordinal #=> "rd"
|
1104
|
+
"11".ordinal #=> "th"
|
1105
|
+
```
|
1106
|
+
|
1107
|
+
####Ordinalize:####
|
1108
|
+
`ordinalize` transforms a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. `Rails Safe`
|
1109
|
+
|
1110
|
+
```ruby
|
1111
|
+
"1".ordinalize #=> "1th"
|
1112
|
+
"2".ordinalize #=> "2nd"
|
1113
|
+
"3".ordinalize #=> "3rd"
|
1114
|
+
"11".ordinalize #=> "4th"
|
1115
|
+
```
|
1116
|
+
|
1117
|
+
####Parameterize:####
|
1118
|
+
`parameterize` and `parameterize!` makes string suitable for a dashed url parameter string. `Rails Safe`
|
1119
|
+
|
1120
|
+
```ruby
|
1121
|
+
"example_string".parameterize #=> "example-string"
|
1122
|
+
"example_string".parameterize("?") #=> "example?string"
|
1123
|
+
```
|
1124
|
+
|
1125
|
+
####Pollute:####
|
1126
|
+
`pollute` pollutes the space between every letter in a string, so it will be exempt from any impending string searches.
|
1127
|
+
|
1128
|
+
```ruby
|
1129
|
+
"test".pollute #=> "t^--^--^e^--^--^s^--^--^t^--^--^"
|
1130
|
+
"test".pollute("-") #=> "t-e-s-t-"
|
1131
|
+
```
|
1132
|
+
|
1133
|
+
####Remove:####
|
1134
|
+
`remove` and `remove!` removes every instance of a string.
|
1135
|
+
|
1136
|
+
```ruby
|
1137
|
+
"this thing that thing".remove("thing") #=> "this that "
|
1138
|
+
"this thing that them".remove("thing", "them") #=> "this that "
|
1139
|
+
```
|
1140
|
+
|
1141
|
+
####Remove Tags:####
|
1142
|
+
`remove_tags` and `remove_tags!` removes HTML tags from a string.
|
1143
|
+
|
1144
|
+
```ruby
|
1145
|
+
"example".strip_tags #=> "example"
|
1146
|
+
"<a href='http://example.com'>click</a>".strip_tags #=> "click"
|
1147
|
+
"this is <b>bold</b> and <em>emphatic</em>".strip_tags #=> "this is bold and emphatic"
|
1148
|
+
```
|
1149
|
+
|
1150
|
+
####Shift:####
|
1151
|
+
`shift` and `shift!` removes the first instance of a string.
|
1152
|
+
|
1153
|
+
```ruby
|
1154
|
+
"this thing that thing".remove("thing") #=> "this that thing"
|
1155
|
+
"this thing that thing".remove("this", "that") #=> " thing thing"
|
1156
|
+
```
|
1157
|
+
|
1158
|
+
####Slugify:####
|
1159
|
+
`slugify` and `slugify!` generates a permalink-style string, with odd characters removed.
|
1160
|
+
|
1161
|
+
```ruby
|
1162
|
+
"example".slugify #=> "example"
|
1163
|
+
"example string".slugify #=> "example-string"
|
1164
|
+
"Example string @@@ test!".slugify #=> "example-string-test"
|
1165
|
+
```
|
1166
|
+
|
1167
|
+
####Squish:####
|
1168
|
+
`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. `Rails Safe`
|
1169
|
+
|
1170
|
+
```ruby
|
1171
|
+
"example string".squish #=> "example string"
|
1172
|
+
"example \n \t string".squish #=> "example string"
|
1173
|
+
" example string ".squish #=> "example string"
|
1174
|
+
```
|
1175
|
+
|
1176
|
+
####Titleize:####
|
1177
|
+
`titleize` and `titleize!` capitalizes each word in a string. `Rails Safe`
|
1178
|
+
|
1179
|
+
```ruby
|
1180
|
+
"example string".titleize #=> "Example String"
|
1181
|
+
"example_string".titleize #=> "Example String"
|
1182
|
+
"ExampleString".titleize #=> "Example String"
|
1183
|
+
```
|
1184
|
+
|
1185
|
+
####To:####
|
1186
|
+
`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. `Rails Safe`
|
1187
|
+
|
1188
|
+
```ruby
|
1189
|
+
"example".to(0) #=> "example"
|
1190
|
+
"example".to(3) #=> "exam"
|
1191
|
+
"example".to(-2) #=> "exampl"
|
1192
|
+
```
|
1193
|
+
|
1194
|
+
####Truncate:####
|
1195
|
+
`truncate` a given text after a given length if text is longer than length. `Rails Safe`
|
1196
|
+
|
1197
|
+
**Options**
|
1198
|
+
* omission: "..."
|
1199
|
+
* separator: " "
|
1200
|
+
|
1201
|
+
```ruby
|
1202
|
+
"example string".truncate(3) #=> "..."
|
1203
|
+
"example string".truncate(6) #=> "exa..."
|
1204
|
+
"example string".truncate(12, separator: " ") #=> "example..."
|
1205
|
+
"example string".truncate(13, omission: "... (more)") #=> "exa... (more)"
|
1206
|
+
"example string".truncate(15) #=> "example string"
|
1207
|
+
```
|
1208
|
+
|
1209
|
+
####Truncate Words:####
|
1210
|
+
`truncate_words` truncates a given text after a given number of words. `Rails Safe`
|
1211
|
+
|
1212
|
+
**Options**
|
1213
|
+
* omission: "..."
|
1214
|
+
* separator: " "
|
1215
|
+
|
1216
|
+
```ruby
|
1217
|
+
"example string test".truncate_words(1) #=> "example..."
|
1218
|
+
'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..."
|
1219
|
+
'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)') #=> "And they found that many... (continued)"
|
1220
|
+
```
|
1221
|
+
|
1222
|
+
####Underscore:####
|
1223
|
+
`underscore` and `underscore!` transforms a string to snakecase. `Rails Safe`
|
1224
|
+
|
1225
|
+
```ruby
|
1226
|
+
"ExampleString".underscore #=> "example_string"
|
1227
|
+
"exampleString".underscore #=> "example_string"
|
1228
|
+
"ExampleString::Test".underscore #=> "example_string/test"
|
1229
|
+
```
|
1230
|
+
|
1231
|
+
####Unpollute:####
|
1232
|
+
`unpollute` removes the default or custom pollution character. Can also be used to remove an unwanted character.
|
1233
|
+
|
1234
|
+
```ruby
|
1235
|
+
"t^--^--^e^--^--^s^--^--^t^--^--^".unpollute #=> "test"
|
1236
|
+
"t-e-s-t-".unpollute #=> "test"
|
1237
|
+
```
|
1238
|
+
|
1239
|
+
####Upcase:####
|
1240
|
+
`upcase?` returns true if all characters are uppercase.
|
1241
|
+
|
1242
|
+
```ruby
|
1243
|
+
"EXAMPLE".upcase? #=> true
|
1244
|
+
"example".upcase? #=> false
|
1245
|
+
"Example".upcase? #=> false
|
1246
|
+
```
|
1247
|
+
|
1248
|
+
### Time
|
1249
|
+
|
1250
|
+
####Format:####
|
1251
|
+
`format` converts a Date or Time object to format it using a human readable string.
|
1252
|
+
|
1253
|
+
**Rules**
|
1254
|
+
* Characters: a-z 0-9 _
|
1255
|
+
* Characters can only be used to generate a format part
|
1256
|
+
|
1257
|
+
```ruby
|
1258
|
+
Time.now.format("year") #=> "2014"
|
1259
|
+
Time.now.format("month_name day, year hour:minute ampm") #=> "January 09, 2014 02:31 pm"
|
1260
|
+
```
|
1261
|
+
|
1262
|
+
| Name | Key | Equivalent `strftime` | Result |
|
1263
|
+
| --- | --- | --- | --- |
|
1264
|
+
| Month - digits zero-padded | `m` or `month` or `month_zero` | %m | (01..12) |
|
1265
|
+
| Month - digits unpadded | `mm` or `Month` or `month_unpadded` | %-m | (1..12) |
|
1266
|
+
| Month - digits blank-padded | `mmm` or `MONTH` or `day_blank` | %_m | ( 1..12) |
|
1267
|
+
| Month - name | `mmmm` or `month_name` | %B | January |
|
1268
|
+
| Month - name abbreviated | `mmmmm` or `month_name_abbr` | %b | Jan |
|
1269
|
+
| Day - digits zero-padded | `d` or `day` or `day_zero` | %d | (01..31) |
|
1270
|
+
| Day - digits unpadded | `dd` or `Day` or `day_unpadded` | %-d | (1..31) |
|
1271
|
+
| Day - digits blank-padded | `ddd` or `DAY` or `day_blank` | %_d | ( 1..31) |
|
1272
|
+
| Day - digits of the year | `dddd` or `day_of_the_year` | %j | (001..366) |
|
1273
|
+
| Week - starting monday | `wwwww` or `week` | %M | (00..53) |
|
1274
|
+
| Week - starting sunday | `wwwwww` or `weekday_offset` | %M | (00..53) |
|
1275
|
+
| Weekday - starting monday | `w` or `weekday` | %M | (1..7) |
|
1276
|
+
| Weekday - starting sunday | `ww` or `weekday` | %M | (0..6) |
|
1277
|
+
| Weekday - name | `www` or `weekday_name` | %M | Sunday |
|
1278
|
+
| Weekday - name abbreviated | `wwww` or `weekday_name_abbr` | %M | Sun |
|
1279
|
+
| Year - digits two | `yy` or `yr` | %y | (00..99) |
|
1280
|
+
| Year - digits four | `yyyy` or `year` | %Y | 1999 |
|
1281
|
+
| Hour - digits zero-padded | `h` or `hour` or `hour_zero` | %H | (00..23) |
|
1282
|
+
| Hour - digits blank-padded | `hh` or `HOUR` or `hour_blank` | %k | ( 0..23) |
|
1283
|
+
| Hour - digits zero-padded | `hhh` or `hour_imperical` or `hour_imperical_zero` | %I | (01..12) |
|
1284
|
+
| Hour - digits blank-padded | `hhhh` or `HOUR_IMPERICAL` or `hour_imperical_blank` | %l | ( 1..12) |
|
1285
|
+
| Minute - minute | `n` or `minute` | %M | (00..59) |
|
1286
|
+
| Second - second | `s` or `second` | %S | (00..60) |
|
1287
|
+
| Meridian - lowercase | `ampm` or `meridian` | %p | am..pm |
|
1288
|
+
| Meridian - uppercase | `AMPM` or `MERIDIAN` | %P | AM..PM |
|
1289
|
+
| Time Zone - time zone | `z` or `time_zone` | %z | +0900 |
|
1290
|
+
| Time Zone - hour and minute offset | `zz` or `time_zone_offset` | %z | +09:00 |
|
1291
|
+
| Time Zone - hour, minute and second offset | `zzz` or `time_zone_offset_full` | %z | +09:00:00 |
|
1292
|
+
|
1293
|
+
####To Format:####
|
1294
|
+
`to_format` converts a Date or Time object to a predefined format.
|
1295
|
+
|
1296
|
+
**For a full list check out the time extention file.**
|
1297
|
+
|
1298
|
+
```ruby
|
1299
|
+
Time.now.to_format(:year) #=> "2014"
|
1300
|
+
Time.now.to_format(:datetime) #=> "January 09, 2014 02:31 pm"
|
1301
|
+
```
|
1302
|
+
|
1303
|
+
| Name | Key | Equivalent `strftime` | Result |
|
1304
|
+
| --- | --- | --- | --- |
|
1305
|
+
| Month - digits zero-padded | `:month` or `:month_zero` | %A | (01..12) |
|
1306
|
+
| Month - digits unpadded | `:month_unpadded` | %a | (1..12) |
|
1307
|
+
| Month - digits blank-padded | `:month_blank` | %a | ( 1..12) |
|
1308
|
+
| Month - name | `:month_name` | %A | January |
|
1309
|
+
| Month - name abbreviated | `:month_name_abbr` | %a | Jan |
|
1310
|
+
| Weekday - digits zero-padded | `:weekday_zero` | %A | (01..31) |
|
1311
|
+
| Weekday - digits unpadded | `:weekday_unpadded` | %a | (1..31) |
|
1312
|
+
| Weekday - digits blank-padded | `:weekday_blank` | %a | ( 1..31) |
|
1313
|
+
| Weekday - name | `:weekday_name` | %A | Sunday |
|
1314
|
+
| Weekday - name abbreviated | `:weekday_name_abbr` | %a | Sun |
|
1315
|
+
| Year - digits two | `:yr` | %y | (00..99) |
|
1316
|
+
| Year - digits four | `:year` | %Y | 1999 |
|
1317
|
+
| Hour - digits zero-padded | `:hour` or `:hour_zero` | %H | (00..23) |
|
1318
|
+
| Hour - digits blank-padded | `:hour_blank` | %k | ( 0..23) |
|
1319
|
+
| Hour - digits zero-padded imperical | `:hour_imperical_zero` | %I | (01..12) |
|
1320
|
+
| Hour - digits blank-padded imperical | `:hour_imperical_blank` | %l | ( 1..12) |
|
1321
|
+
| Minute - minute | `:minute` | %M | (00..59) |
|
1322
|
+
| Second - second | `:second` | %S | (00..60) |
|
1323
|
+
| Time Zone - time zone | `:time_zone` | %z | +0900 |
|
1324
|
+
| Time Zone - hour and minute offset | `:time_zone_offset` | %z | +09:00 |
|
1325
|
+
| Time Zone - hour, minute and second offset | `:time_zone_offset_full` | %z | +09:00:00 |
|
1326
|
+
| Date - name | `:date` | %B %-d, %Y | January 9, 2014 |
|
1327
|
+
| Date - name abbreviated | `:date_abbr` | %b %-d, %Y | Jan 9, 2014 |
|
1328
|
+
| Date - iso | `:date_iso` | %Y-%m-%d | 2014-01-09 |
|
1329
|
+
| Datetime - name | `:datetime` | %B %-d, %Y %H:%M | January 9, 2014 00:31 |
|
1330
|
+
| Datetime - name abbreviated | `:datetime_abbr` | %b %-d, %Y %H:%M | Jan 9, 2014 00:31 |
|
1331
|
+
| Datetime - iso | `:datetime_iso` | %Y-%m-%d %H:%M | 2014-01-09 00:31 |
|
1332
|
+
| Datetime - name imperical | `:datetime_imperical` | %B %-d, %Y %H:%M | January 9, 2014 12:31 am |
|
1333
|
+
| Datetime - name abbreviated imperical | `:datetime_imperical_abbr` | %b %-d, %Y %H:%M | Jan 9, 2014 12:31 am |
|
1334
|
+
| Datetime - iso imperical | `:datetime_imperical_iso` | %Y-%m-%d %H:%M | 2014-01-09 12:31 am |
|
1335
|
+
| Datetime - name time zone | `:datetime_tzn` | %B %-d, %Y %H:%M %Z | January 9, 2014 00:31 UTC |
|
1336
|
+
| Datetime - name abbreviated time zone | `:datetime_abbr_tzn` | %b %-d, %Y %H:%M %Z | Jan 9, 2014 00:31 UTC |
|
1337
|
+
| Datetime - iso time zone | `:datetime_iso_tzn` | %Y-%m-%d %H:%M %z | 2014-01-09 00:31 +0000 |
|
1338
|
+
| Datetime - name imperical time zone | `:datetime_imperical_tzn` | %B %-d, %Y %H:%M %Z | January 9, 2014 12:31 am UTC |
|
1339
|
+
| Datetime - name abbreviated imperical time zone | `:datetime_imperical_abbr_tzn` | %b %-d, %Y %H:%M %Z | Jan 9, 2014 12:31 am UTC |
|
1340
|
+
| Datetime - iso imperical time zone | `:datetime_imperical_iso_tzn` | %Y-%m-%d %H:%M %z | 2014-01-09 12:31 am +0000 |
|
1341
|
+
| Day - name | `:day` | %B %-d | January 9 |
|
1342
|
+
| Day - name abbreviated | `:day_abbr` | %b %-d | Jan 9 |
|
1343
|
+
| Day - iso | `:day_iso` | %m-%d | 01-09 |
|
1344
|
+
| Daytime - name | `:daytime` | %B %-d %H:%M | January 9 00:31 |
|
1345
|
+
| Daytime - name abbreviated | `:daytime_abbr` | %b %-d %H:%M | Jan 9 00:31 |
|
1346
|
+
| Daytime - iso | `:daytime_iso` | %m-%d %H:%M | 01-09 00:31 |
|
1347
|
+
| Daytime - name imperical | `:daytime_imperical` | %B %-d %H:%M | January 9 12:31 am |
|
1348
|
+
| Daytime - name abbreviated imperical | `:daytime_imperical_abbr` | %b %-d %H:%M | Jan 9 12:31 am |
|
1349
|
+
| Daytime - iso imperical | `:daytime_imperical_iso` | %m-%d %H:%M | 01-09 12:31 am |
|
1350
|
+
| Time - zero-padded | `:time` or `:time_zero` | %H:%M | 00:31 |
|
1351
|
+
| Time - blank-padded | `:time_blank` | %k:%M %z | 0:31 |
|
1352
|
+
| Time - with time zone | `:time_tz` | %H:%M %z | 00:31 +0000 |
|
1353
|
+
| Time - with time zone name | `:time_tzn` | %H:%M %Z | 00:31 UTC |
|
1354
|
+
|
1355
|
+
## Contributing
|
1356
|
+
|
1357
|
+
1. Fork it ( http://github.com/<my-github-username>/active_object/fork )
|
1358
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
1359
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
1360
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
1361
|
+
5. Create new Pull Request
|