more_math 1.2.0 → 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: bb1b7eaf071255d2a996c85a1f46394a21e30178a74940d8b88f7c538dac094b
4
- data.tar.gz: d6f4f436f85fd44dd57751190d1ba85f458b07eecc07a3fa78c7f1de69c3b160
3
+ metadata.gz: 01200b0474b98510b8405a12492684e8d871d3a406a697c2d7775a26efe8c2ae
4
+ data.tar.gz: dfa50772aedbc2f2530e35e7c1825a128cdaa5a9a76781d12484c9b8a2a995f9
5
5
  SHA512:
6
- metadata.gz: 104e595a14fbb58f014ea5cd944552c8b85401c0d3bc2243489457cfbf3d5227104f7da6f1a891b21a551e7e11f830d100b2172593ba2932675a30f0dfb20200
7
- data.tar.gz: 44320e2edaa82a99f5629562130a807ccc4e3499cba750f8d564da8515aabcec363e182d76bbe86c0af7e8d587691703e87a1d0743016c3d6fbf4ccb2d00f934
6
+ metadata.gz: c1dc76b84fed3a181158201798ce5fe6bff1ac22b2bff104b2f22ff5186104217c65ad1425cd676959f2358fc1b107722c3bc9bfcbd2ca6060fc033d927b47e6
7
+ data.tar.gz: 90ad74b6b12f7735b088dc6bd06ccfbbe02af132718240a81eb8080c550a04c856698394c821441ddac4f7188524d02a006bf3579645a5b25a483a8e0dac568e
data/CHANGES.md CHANGED
@@ -1,5 +1,20 @@
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
+
12
+ ## 2024-09-30 v1.2.1
13
+
14
+ * Refactor histogram display logic for utf8 and ascii bars
15
+ + *Improved `utf8_bar` method to handle fractional bar widths.*
16
+ + *Updated test case in `histogram_test.rb` to reflect changes.*
17
+
3
18
  ## 2024-09-30 v1.2.0
4
19
 
5
20
  #### Significant Changes
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'
@@ -53,12 +53,16 @@ module MoreMath
53
53
  def utf8_bar(bar_width)
54
54
  fract = bar_width - bar_width.floor
55
55
  bar = ?⣿ * bar_width.floor
56
- fract > 0.5 and bar << ?⡇
56
+ if fract > 0.5
57
+ bar << ?⡇
58
+ else
59
+ bar << ' '
60
+ end
57
61
  bar
58
62
  end
59
63
 
60
64
  def ascii_bar(bar_width)
61
- bar = ?* * bar_width
65
+ ?* * bar_width
62
66
  end
63
67
 
64
68
  def utf8?
@@ -68,21 +72,33 @@ module MoreMath
68
72
  def output_row(row, width)
69
73
  left, right, count = row
70
74
  if @with_counts
71
- c = utf8? ? 2 : 1
72
- left_width = width - (counts.map { |x| x.to_s.size }.max + c)
75
+ output_row_with_count(left, right, count, width)
73
76
  else
74
- left_width = width
77
+ output_row_without_count(left, right, count, width)
75
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)
76
84
  if left_width < 0
77
85
  left_width = width
78
86
  end
79
87
  factor = left_width.to_f / max_count
80
88
  bar_width = (count * factor)
81
89
  bar = utf8? ? utf8_bar(bar_width) : ascii_bar(bar_width)
82
- if @with_counts
83
- bar += count.to_s.rjust(width - bar_width)
84
- end
85
- "%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 ]
86
102
  end
87
103
 
88
104
  def rows
@@ -1,6 +1,6 @@
1
1
  module MoreMath
2
2
  # MoreMath version
3
- VERSION = '1.2.0'
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.0 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.0".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,9 +77,9 @@ 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 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇
65
- 1.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
80
+ 7.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
81
+ 4.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇
82
+ 1.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
66
83
  max_count=420
67
84
  end
68
85
  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.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank