Class: MoreMath::Histogram
Overview
A histogram gives an overview of a sequence’s elements.
Defined Under Namespace
Classes: Bin
Instance Attribute Summary collapse
-
#bins ⇒ Object
readonly
Number of bins for this Histogram.
Instance Method Summary collapse
- #counts ⇒ Object
-
#display(output = $stdout, width = 65) ⇒ Object
Display this histogram to
output
,width
is the parameter forprepare_display
. - #each_bin(&block) ⇒ Object
-
#initialize(sequence, arg = 10) ⇒ Histogram
constructor
Create a Histogram for the elements of
sequence
withbins
bins. - #max_count ⇒ Object
- #terminal_width ⇒ Object
-
#to_a ⇒ Object
Return the computed histogram as an array of Bin objects.
Constructor Details
#initialize(sequence, arg = 10) ⇒ Histogram
Create a Histogram for the elements of sequence
with bins
bins.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/more_math/histogram.rb', line 9 def initialize(sequence, arg = 10) @with_counts = false if arg.is_a?(Hash) bins = arg.fetch(:bins, 10) wc = arg[:with_counts] and @with_counts = wc else bins = arg end @sequence = sequence @bins = bins @result = compute end |
Instance Attribute Details
#bins ⇒ Object (readonly)
Number of bins for this Histogram.
23 24 25 |
# File 'lib/more_math/histogram.rb', line 23 def bins @bins end |
Instance Method Details
#counts ⇒ Object
34 35 36 |
# File 'lib/more_math/histogram.rb', line 34 def counts each_bin.map(&:count) end |
#display(output = $stdout, width = 65) ⇒ Object
Display this histogram to output
, width
is the parameter for prepare_display
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/more_math/histogram.rb', line 40 def display(output = $stdout, width = 65) if width.is_a?(String) && width =~ /(.+)%\z/ percentage = Float($1).clamp(0, 100) width = (terminal_width * (percentage / 100.0)).floor end width > 15 or raise ArgumentError, "width needs to be >= 15" for r in rows output << output_row(r, width) end output << "max_count=#{max_count}\n" self end |
#each_bin(&block) ⇒ Object
30 31 32 |
# File 'lib/more_math/histogram.rb', line 30 def each_bin(&block) @result.each(&block) end |
#max_count ⇒ Object
57 58 59 |
# File 'lib/more_math/histogram.rb', line 57 def max_count counts.max end |
#terminal_width ⇒ Object
53 54 55 |
# File 'lib/more_math/histogram.rb', line 53 def terminal_width Tins::Terminal.columns end |
#to_a ⇒ Object
Return the computed histogram as an array of Bin objects.
26 27 28 |
# File 'lib/more_math/histogram.rb', line 26 def to_a @result end |