string-utility 2.7.0 → 2.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/utils.rb +16 -20
- data/spec/benchmark.rb +5 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43632282870b5f9d39eb984e7f8e9078d09e85d5
|
4
|
+
data.tar.gz: b471b51a2f3ff879251cda80ddea7ce86e8e67c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c72f0bf37646c2c3363eed1e2bff1dbd9f195fd2649581800677893e4f046249f38747a8b13034f5cb05994246a02dd4981afef2ba53c624cd16880966538b7f
|
7
|
+
data.tar.gz: 63d7eab095e0f1638dc47d94bea54c994568e0dbaf493f885486cc79ef74392779faaf8f472a3e819e61a5c7850495751b1f1c199d51c5ee998e3017bd0035ff
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Changelog
|
2
2
|
## Version 2
|
3
|
+
### Version 2.7.1
|
4
|
+
* Improve random_line performance by approximately 1 second real time.
|
5
|
+
* Improve random_color performance by about 400-500%.
|
6
|
+
* Improve separate performance by about 250%.
|
7
|
+
|
3
8
|
### Version 2.7.0
|
4
9
|
* Improve random_line by not dividing by 0.
|
5
10
|
* Add safely_gsub! refinement method for an easier, more readable version of `str.gsub!(pattern, replace) || str`.
|
data/lib/utils.rb
CHANGED
@@ -8,16 +8,12 @@ module StringUtility
|
|
8
8
|
# 1 character long. Defaults to a comma.
|
9
9
|
# @return [String] A new formatted version of the provided string.
|
10
10
|
def separate(count = 3, separator = ',')
|
11
|
-
separator
|
12
|
-
string = self
|
13
|
-
string.reverse!
|
14
|
-
string = string.scan(/.{1,#{count}}/).join(separator)
|
15
|
-
string.reverse!
|
11
|
+
chars.reverse!.each_slice(count).map(&:join).join(separator[0]).reverse!
|
16
12
|
end
|
17
13
|
|
18
14
|
# Converts a separated string into an integer. This is basically the reverse
|
19
15
|
# of #separate.
|
20
|
-
# @return [
|
16
|
+
# @return [Integer] The integer version of the separated string.
|
21
17
|
def to_i_separated
|
22
18
|
safely_gsub!(/\D/, '').to_i
|
23
19
|
end
|
@@ -47,37 +43,37 @@ module StringUtility
|
|
47
43
|
extend self
|
48
44
|
|
49
45
|
# Gets a random line in a file.
|
46
|
+
# This will include newlines and similar characters, so you may want to chomp the result.
|
50
47
|
# @param path [String] The path to the file.
|
51
48
|
# @return [String] A random line in the file.
|
52
49
|
def self.random_line(path)
|
53
|
-
|
54
|
-
|
55
|
-
file.each_with_index do |line, num|
|
56
|
-
int = num == 0 ? 1.0 : 1.0 / num
|
57
|
-
selected = line if !line.nil? && rand < int
|
58
|
-
end
|
59
|
-
file.close
|
60
|
-
selected
|
50
|
+
lines = File.readlines(path)
|
51
|
+
lines[rand(lines.count)]
|
61
52
|
end
|
62
53
|
|
63
54
|
# Gets a random three-digit hexadecimal web color string.
|
64
55
|
# @return [String] A random hexadecimal.
|
65
56
|
def self.random_color_three
|
66
|
-
|
67
|
-
"##{string}"
|
57
|
+
random_color(3)
|
68
58
|
end
|
69
59
|
|
70
60
|
# Gets a random six-digit hexadecimal web color string.
|
71
61
|
# @return [String] See #random_color_three
|
72
62
|
def self.random_color_six
|
73
|
-
|
74
|
-
"##{string}"
|
63
|
+
random_color(6)
|
75
64
|
end
|
76
65
|
|
77
66
|
private
|
78
67
|
|
68
|
+
STRING_VALS = ('A'..'F').to_a.freeze
|
69
|
+
INT_VALS = (0..9).to_a.freeze
|
70
|
+
COMBINED_VALS = (STRING_VALS + INT_VALS).freeze
|
71
|
+
|
79
72
|
def random_color(limit)
|
80
|
-
|
81
|
-
limit.
|
73
|
+
str = '#'
|
74
|
+
limit.times do
|
75
|
+
str << COMBINED_VALS.sample.to_s
|
76
|
+
end
|
77
|
+
str
|
82
78
|
end
|
83
79
|
end
|
data/spec/benchmark.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string-utility
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.7.
|
4
|
+
version: 2.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eli Foster
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: " Some simple but handy methods to interact with string objects.\n"
|
14
14
|
email: elifosterwy@gmail.com
|