Class: ToHistogram::Histogram

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/histogram.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_widthObject (readonly)

Returns the value of attribute bucket_width.



16
17
18
# File 'lib/histogram.rb', line 16

def bucket_width
  @bucket_width
end

#bucketsObject (readonly)

Returns the value of attribute buckets.



16
17
18
# File 'lib/histogram.rb', line 16

def buckets
  @buckets
end

#num_bucketsObject (readonly)

Returns the value of attribute num_buckets.



16
17
18
# File 'lib/histogram.rb', line 16

def num_buckets
  @num_buckets
end

#percentileObject (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_lengthObject



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_valuesObject



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

#inspectObject



42
43
44
# File 'lib/histogram.rb', line 42

def inspect
  return "class: #{self.class} object_id: #{self.object_id}"
end

#lengthObject



28
29
30
# File 'lib/histogram.rb', line 28

def length
  return @buckets.length
end