Class: Stan::Histogram

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list, bin_size: nil) ⇒ Histogram

Returns a new instance of Histogram.



5
6
7
8
# File 'lib/stan/histogram.rb', line 5

def initialize(list, bin_size: nil)
  @list = list
  @bin_size = bin_size
end

Instance Attribute Details

#bin_sizeObject (readonly)

Returns the value of attribute bin_size.



3
4
5
# File 'lib/stan/histogram.rb', line 3

def bin_size
  @bin_size
end

#listObject (readonly)

Returns the value of attribute list.



3
4
5
# File 'lib/stan/histogram.rb', line 3

def list
  @list
end

Instance Method Details

#to_aObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/stan/histogram.rb', line 10

def to_a
  @list.each_with_object({}) do |item, hash|
    bin =
      if bin_size.to_f > 0
        (item.to_f / bin_size).round * bin_size
      else
        item
      end

    hash[bin] = hash[bin].to_i + 1
  end.sort
end

#to_hObject



23
24
25
# File 'lib/stan/histogram.rb', line 23

def to_h
  to_a.to_h
end