more_math 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2945331f84849efc01cafcdd9dec0e1402a6296c979d6b6d5ecb41ff49427a2
4
- data.tar.gz: 2e25d9b28a5c326eb50d7ca12ef66d0562f8e5839a8f9c37a9db504fc13c286c
3
+ metadata.gz: 01200b0474b98510b8405a12492684e8d871d3a406a697c2d7775a26efe8c2ae
4
+ data.tar.gz: dfa50772aedbc2f2530e35e7c1825a128cdaa5a9a76781d12484c9b8a2a995f9
5
5
  SHA512:
6
- metadata.gz: 11a42069a9e921c857849cceb0f912a3989a4bf046645be765c8acb54f9bea299ced8790be585fd04367531e6484847866cde89486c7aa6943fb396d80bfcf31
7
- data.tar.gz: ee3e2b0ba782557eb94770833b0e98494254ce1a138caf493e72d208b54f56bb62645bd0779e71c84b32b949df2b8de3f4424c2c107b3c609a7bd5901a2cd471
6
+ metadata.gz: c1dc76b84fed3a181158201798ce5fe6bff1ac22b2bff104b2f22ff5186104217c65ad1425cd676959f2358fc1b107722c3bc9bfcbd2ca6060fc033d927b47e6
7
+ data.tar.gz: 90ad74b6b12f7735b088dc6bd06ccfbbe02af132718240a81eb8080c550a04c856698394c821441ddac4f7188524d02a006bf3579645a5b25a483a8e0dac568e
data/CHANGES.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changes
2
2
 
3
+ ## 2024-09-30 v1.2.2
4
+
5
+ ### Improvements
6
+ * Refactor Histogram display logic for better UTF-8 support:
7
+ + Extracted `output_row_with_count` and `output_row_without_count` methods
8
+ + Updated test cases for histogram display with counts and UTF-8 support
9
+ * Update Rakefile to ignore `.utilsrc` file:
10
+ - Add `.utilsrc` to `package_ignore` list in Rakefile
11
+
3
12
  ## 2024-09-30 v1.2.1
4
13
 
5
14
  * Refactor histogram display logic for utf8 and ascii bars
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ GemHadar do
14
14
  '.AppleDouble', 'tags', '.byebug_history', '.DS_Store'
15
15
  readme 'README.md'
16
16
  title "#{name.camelize} -- More Math in Ruby"
17
- package_ignore '.all_images.yml', '.gitignore', 'VERSION',
17
+ package_ignore '.all_images.yml', '.gitignore', 'VERSION', '.utilsrc',
18
18
  *Dir.glob('.github/**/*', File::FNM_DOTMATCH)
19
19
 
20
20
  required_ruby_version '>= 2.0'
@@ -62,7 +62,7 @@ module MoreMath
62
62
  end
63
63
 
64
64
  def ascii_bar(bar_width)
65
- bar = ?* * bar_width
65
+ ?* * bar_width
66
66
  end
67
67
 
68
68
  def utf8?
@@ -72,29 +72,33 @@ module MoreMath
72
72
  def output_row(row, width)
73
73
  left, right, count = row
74
74
  if @with_counts
75
- c = utf8? ? 2 : 1
76
- left_width = width - (counts.map { |x| x.to_s.size }.max + c)
75
+ output_row_with_count(left, right, count, width)
77
76
  else
78
- left_width = width
77
+ output_row_without_count(left, right, count, width)
79
78
  end
79
+ end
80
+
81
+ def output_row_with_count(left, right, count, width)
82
+ c = utf8? ? 2 : 1
83
+ left_width = width - (counts.map { |x| x.to_s.size }.max + c)
80
84
  if left_width < 0
81
85
  left_width = width
82
86
  end
83
87
  factor = left_width.to_f / max_count
84
88
  bar_width = (count * factor)
85
89
  bar = utf8? ? utf8_bar(bar_width) : ascii_bar(bar_width)
86
- if @with_counts
87
- if utf8?
88
- if bar[-1] == ' '
89
- bar += count.to_s.rjust(width - bar_width - 1)
90
- else
91
- bar += count.to_s.rjust(width - bar_width)
92
- end
93
- else
94
- bar += count.to_s.rjust(width - bar_width)
95
- end
96
- end
97
- "%11.5f -|%s\n" % [ (left + right) / 2.0, bar ]
90
+ max_count_length = max_count.to_s.size
91
+ "%11.5f -|%#{-width + max_count_length}s%#{max_count_length}s\n" %
92
+ [ (left + right) / 2.0, bar, count ]
93
+ end
94
+
95
+ def output_row_without_count(left, right, count, width)
96
+ left_width = width
97
+ left_width < 0 and left_width = width
98
+ factor = left_width.to_f / max_count
99
+ bar_width = (count * factor)
100
+ bar = utf8? ? utf8_bar(bar_width) : ascii_bar(bar_width)
101
+ "%11.5f -|%#{-width}s\n" % [ (left + right) / 2.0, bar ]
98
102
  end
99
103
 
100
104
  def rows
@@ -1,6 +1,6 @@
1
1
  module MoreMath
2
2
  # MoreMath version
3
- VERSION = '1.2.1'
3
+ VERSION = '1.2.2'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
data/more_math.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: more_math 1.2.1 ruby lib
2
+ # stub: more_math 1.2.2 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "more_math".freeze
6
- s.version = "1.2.1".freeze
6
+ s.version = "1.2.2".freeze
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
@@ -25,7 +25,7 @@ class HistogramTest < Test::Unit::TestCase
25
25
  output = StringIO.new
26
26
  histogram.display output
27
27
  assert_equal <<~end, output.string
28
- 2.50000 -|*************************
28
+ 2.50000 -|*************************
29
29
  1.50000 -|**************************************************
30
30
  0.50000 -|**************************************************
31
31
  max_count=2
@@ -50,6 +50,23 @@ class HistogramTest < Test::Unit::TestCase
50
50
  end
51
51
  end
52
52
 
53
+ def test_histogram_display_with_counts_lots
54
+ srand 1337
55
+ sequence = Sequence.new 1000.times.map { rand(10) * rand(10) }
56
+ histogram = Histogram.new sequence, with_counts: true, bins: 3
57
+ def histogram.utf8?
58
+ false
59
+ end
60
+ output = StringIO.new
61
+ histogram.display output
62
+ assert_equal <<~end, output.string
63
+ 67.50000 -|***** 81
64
+ 40.50000 -|************* 206
65
+ 13.50000 -|********************************************** 713
66
+ max_count=713
67
+ end
68
+ end
69
+
53
70
  def test_histogram_display_utf8
54
71
  srand 1337
55
72
  sequence = Sequence.new 1000.times.map { rand(10) }
@@ -60,8 +77,8 @@ class HistogramTest < Test::Unit::TestCase
60
77
  output = StringIO.new
61
78
  histogram.display output
62
79
  assert_equal <<~end, output.string
63
- 7.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
64
- 4.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇
80
+ 7.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
81
+ 4.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇
65
82
  1.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
66
83
  max_count=420
67
84
  end
@@ -83,4 +100,29 @@ class HistogramTest < Test::Unit::TestCase
83
100
  max_count=420
84
101
  end
85
102
  end
103
+
104
+ def test_histogram_display_with_counts_utf8_zero
105
+ srand 1337
106
+ sequence = Sequence.new 1000.times.map { rand(10) * rand(10) }
107
+ histogram = Histogram.new sequence, with_counts: true, bins: 3
108
+ def histogram.utf8?
109
+ true
110
+ end
111
+ output = StringIO.new
112
+ histogram.display output
113
+ assert_equal <<~end, output.string
114
+ 67.50000 -|⣿⣿⣿⣿⣿ 81
115
+ 40.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ 206
116
+ 13.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ 713
117
+ max_count=713
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ def output_histogram(histogram)
124
+ $stdout.puts
125
+ histogram.display $stdout
126
+ $stdout.puts
127
+ end
86
128
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: more_math
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank