string-utility 2.7.0 → 2.7.1

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/lib/utils.rb +16 -20
  4. data/spec/benchmark.rb +5 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65f4ab68c518d824992c9a23668fff43c9cc059b
4
- data.tar.gz: e7daf05079663691b8ae629b715c68a4101b8fe7
3
+ metadata.gz: 43632282870b5f9d39eb984e7f8e9078d09e85d5
4
+ data.tar.gz: b471b51a2f3ff879251cda80ddea7ce86e8e67c7
5
5
  SHA512:
6
- metadata.gz: 481e28a768310e5e4926a1b7bacb3d76a02dc88962abe2efe595298a595f9a6866017c1ed5a864232f3181d3e9f15d3528a45034294b6751ad58b3357a304fb9
7
- data.tar.gz: fb0a4e78fa9ef435947020a9f9f9e798f616a601d0f792dacf5283dc84053212aa873206feddb82b35f55199cb3d481929dc20112565520994b12740f83248fc
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 = separator[0]
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 [Int] The integer version of the separated string.
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
- file = open(path)
54
- selected = nil
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
- string = random_color(0..2)
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
- string = random_color(0..5)
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
- values = (('A'..'F').to_a + (0..9).to_a)
81
- limit.map { values.sample }.join
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
@@ -1,5 +1,9 @@
1
1
  require 'benchmark'
2
- require_relative '../lib/utils'
2
+ if ARGV.include?('--gem')
3
+ require 'string-utility'
4
+ else
5
+ require_relative '../lib/utils'
6
+ end
3
7
 
4
8
  TIMES = 100_000
5
9
  using StringUtility
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.0
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-18 00:00:00.000000000 Z
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