string-utility 2.6.1 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/utils.rb +19 -12
- data/spec/benchmark.rb +33 -0
- data/spec/testcases.rb +7 -0
- metadata +4 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65f4ab68c518d824992c9a23668fff43c9cc059b
|
4
|
+
data.tar.gz: e7daf05079663691b8ae629b715c68a4101b8fe7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 481e28a768310e5e4926a1b7bacb3d76a02dc88962abe2efe595298a595f9a6866017c1ed5a864232f3181d3e9f15d3528a45034294b6751ad58b3357a304fb9
|
7
|
+
data.tar.gz: fb0a4e78fa9ef435947020a9f9f9e798f616a601d0f792dacf5283dc84053212aa873206feddb82b35f55199cb3d481929dc20112565520994b12740f83248fc
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Changelog
|
2
2
|
## Version 2
|
3
|
+
### Version 2.7.0
|
4
|
+
* Improve random_line by not dividing by 0.
|
5
|
+
* Add safely_gsub! refinement method for an easier, more readable version of `str.gsub!(pattern, replace) || str`.
|
6
|
+
|
3
7
|
### Version 2.6.1
|
4
8
|
* Fix issue where to_i_separated, underscorify, and spacify returned nil, due to tr! and gsub! returning nil (#2).
|
5
9
|
|
data/lib/utils.rb
CHANGED
@@ -6,9 +6,9 @@ module StringUtility
|
|
6
6
|
# @param separator [String] The string to use as a separator. Can only be
|
7
7
|
# 1 character long. Will use the first character if it is greater than
|
8
8
|
# 1 character long. Defaults to a comma.
|
9
|
-
# @return [String]
|
9
|
+
# @return [String] A new formatted version of the provided string.
|
10
10
|
def separate(count = 3, separator = ',')
|
11
|
-
separator = separator[0]
|
11
|
+
separator = separator[0]
|
12
12
|
string = self
|
13
13
|
string.reverse!
|
14
14
|
string = string.scan(/.{1,#{count}}/).join(separator)
|
@@ -19,22 +19,28 @@ module StringUtility
|
|
19
19
|
# of #separate.
|
20
20
|
# @return [Int] The integer version of the separated string.
|
21
21
|
def to_i_separated
|
22
|
-
|
23
|
-
to_i
|
22
|
+
safely_gsub!(/\D/, '').to_i
|
24
23
|
end
|
25
24
|
|
26
25
|
# Replaces all whitespace with underscores.
|
27
26
|
# @return [String] The string with replaced whitespace.
|
28
27
|
def underscorify
|
29
|
-
|
30
|
-
self
|
28
|
+
safely_gsub!(/\s/, '_')
|
31
29
|
end
|
32
30
|
|
33
31
|
# Replaces all underscores with whitespace.
|
34
32
|
# @return [String] The string with replaced underscores.
|
35
33
|
def spacify
|
36
|
-
|
37
|
-
|
34
|
+
safely_gsub!('_', ' ')
|
35
|
+
end
|
36
|
+
|
37
|
+
# Readable shorthand for the only safe, fast way to use gsub! using || operators. Unlike gsub!, safely_gsub! will
|
38
|
+
# NEVER return nil. This method should not be used instead of gsub! when the string replacement is predictable, as
|
39
|
+
# this method is somewhat slower than gsub! (see spec/benchmark).
|
40
|
+
# @see #{String#gsub!} for parameters and other information.
|
41
|
+
# @return [String] Exactly what gsub! would return, or self. Never nil.
|
42
|
+
def safely_gsub!(pattern, replace)
|
43
|
+
gsub!(pattern, replace) || self
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
@@ -47,20 +53,21 @@ module StringUtility
|
|
47
53
|
file = open(path)
|
48
54
|
selected = nil
|
49
55
|
file.each_with_index do |line, num|
|
50
|
-
|
56
|
+
int = num == 0 ? 1.0 : 1.0 / num
|
57
|
+
selected = line if !line.nil? && rand < int
|
51
58
|
end
|
52
59
|
file.close
|
53
60
|
selected
|
54
61
|
end
|
55
62
|
|
56
|
-
# Gets a random three-digit
|
57
|
-
# @return [String] A random
|
63
|
+
# Gets a random three-digit hexadecimal web color string.
|
64
|
+
# @return [String] A random hexadecimal.
|
58
65
|
def self.random_color_three
|
59
66
|
string = random_color(0..2)
|
60
67
|
"##{string}"
|
61
68
|
end
|
62
69
|
|
63
|
-
# Gets a random six-digit
|
70
|
+
# Gets a random six-digit hexadecimal web color string.
|
64
71
|
# @return [String] See #random_color_three
|
65
72
|
def self.random_color_six
|
66
73
|
string = random_color(0..5)
|
data/spec/benchmark.rb
CHANGED
@@ -46,3 +46,36 @@ Benchmark.bm do |b|
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
|
+
|
50
|
+
Benchmark.bm do |b|
|
51
|
+
b.report('gsub! success') do
|
52
|
+
TIMES.times do
|
53
|
+
'ABC'.gsub!('ABC', '123')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
b.report('safely_gsub! success') do
|
58
|
+
TIMES.times do
|
59
|
+
'ABC'.safely_gsub!('ABC', '123')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
b.report('gsub! fail') do
|
64
|
+
TIMES.times do
|
65
|
+
'ABC'.gsub!('DEF', '456')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
b.report('gsub! fail safe') do
|
70
|
+
TIMES.times do
|
71
|
+
str = 'ABC'
|
72
|
+
str.gsub!('DEF', '456') || str
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
b.report('safely_gsub! fail') do
|
77
|
+
TIMES.times do
|
78
|
+
'ABC'.safely_gsub!('DEF', '456')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/spec/testcases.rb
CHANGED
@@ -8,6 +8,7 @@ class TestStringUtility < Test::Unit::TestCase
|
|
8
8
|
assert_equal('10,00', '1000'.separate(2))
|
9
9
|
assert_equal('1:000', '1000'.separate(3, ':'))
|
10
10
|
assert_equal('10:00', '1000'.separate(2, ':'))
|
11
|
+
assert_equal('100', '100'.separate)
|
11
12
|
end
|
12
13
|
|
13
14
|
def test_to_i_sparated
|
@@ -37,4 +38,10 @@ class TestStringUtility < Test::Unit::TestCase
|
|
37
38
|
def test_random_color_six
|
38
39
|
assert_instance_of(String, StringUtility.random_color_six)
|
39
40
|
end
|
41
|
+
|
42
|
+
def test_safely_gsub
|
43
|
+
assert_equal('b', 'b'.safely_gsub!('a', '1'))
|
44
|
+
assert_not_nil('b'.safely_gsub!('a', '1'))
|
45
|
+
assert_equal('bb', 'ab'.safely_gsub!('a', 'b'))
|
46
|
+
end
|
40
47
|
end
|
metadata
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string-utility
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eli Foster
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
Some simple but handy methods to interact with string objects.
|
13
|
+
description: " Some simple but handy methods to interact with string objects.\n"
|
15
14
|
email: elifosterwy@gmail.com
|
16
15
|
executables: []
|
17
16
|
extensions: []
|
@@ -43,9 +42,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
42
|
version: '0'
|
44
43
|
requirements: []
|
45
44
|
rubyforge_project:
|
46
|
-
rubygems_version: 2.
|
45
|
+
rubygems_version: 2.5.1
|
47
46
|
signing_key:
|
48
47
|
specification_version: 4
|
49
48
|
summary: Provides some basic utilities for interacting with Strings
|
50
49
|
test_files: []
|
51
|
-
has_rdoc:
|