Class: ToHistogram::Histogram
- Inherits:
-
Object
- Object
- ToHistogram::Histogram
- Includes:
- Enumerable
- Defined in:
- lib/histogram.rb
Instance Attribute Summary collapse
-
#bucket_width ⇒ Object
readonly
Returns the value of attribute bucket_width.
-
#buckets ⇒ Object
readonly
Returns the value of attribute buckets.
-
#num_buckets ⇒ Object
readonly
Returns the value of attribute num_buckets.
-
#percentile ⇒ Object
readonly
Returns the value of attribute percentile.
Instance Method Summary collapse
- #[](i) ⇒ Object
- #bucket_contents_length ⇒ Object
- #bucket_contents_values ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(array, num_buckets: 10, bucket_width: 'auto', percentile: 100) ⇒ Histogram
constructor
A new instance of Histogram.
- #inspect ⇒ Object
- #length ⇒ Object
Constructor Details
#initialize(array, num_buckets: 10, bucket_width: 'auto', percentile: 100) ⇒ Histogram
Returns a new instance of Histogram.
8 9 10 11 12 13 14 15 |
# File 'lib/histogram.rb', line 8 def initialize(array, num_buckets: 10, bucket_width: 'auto', percentile: 100) bucketizer = Bucketizer.new(array, num_buckets: num_buckets, bucket_width: bucket_width, percentile: percentile) @buckets = bucketizer.create_buckets @bucket_width = bucketizer.bucket_width @percentile = percentile @num_buckets = num_buckets end |
Instance Attribute Details
#bucket_width ⇒ Object (readonly)
Returns the value of attribute bucket_width.
16 17 18 |
# File 'lib/histogram.rb', line 16 def bucket_width @bucket_width end |
#buckets ⇒ Object (readonly)
Returns the value of attribute buckets.
16 17 18 |
# File 'lib/histogram.rb', line 16 def buckets @buckets end |
#num_buckets ⇒ Object (readonly)
Returns the value of attribute num_buckets.
16 17 18 |
# File 'lib/histogram.rb', line 16 def num_buckets @num_buckets end |
#percentile ⇒ Object (readonly)
Returns the value of attribute percentile.
16 17 18 |
# File 'lib/histogram.rb', line 16 def percentile @percentile end |
Instance Method Details
#[](i) ⇒ Object
24 25 26 |
# File 'lib/histogram.rb', line 24 def [](i) return @buckets[i] end |
#bucket_contents_length ⇒ Object
32 33 34 |
# File 'lib/histogram.rb', line 32 def bucket_contents_length @buckets.reduce(0) { |sum, x| sum + x.contents.length } end |
#bucket_contents_values ⇒ Object
36 37 38 39 40 |
# File 'lib/histogram.rb', line 36 def bucket_contents_values a = [] @buckets.map { |b| a << b.contents } return a.flatten end |
#each(&block) ⇒ Object
18 19 20 21 22 |
# File 'lib/histogram.rb', line 18 def each(&block) @buckets.each do |b| yield b end end |
#inspect ⇒ Object
42 43 44 |
# File 'lib/histogram.rb', line 42 def inspect return "class: #{self.class} object_id: #{self.object_id}" end |
#length ⇒ Object
28 29 30 |
# File 'lib/histogram.rb', line 28 def length return @buckets.length end |