Class: ToBarGraph::BarGraph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ BarGraph

Returns a new instance of BarGraph.



8
9
10
11
# File 'lib/bar_graph.rb', line 8

def initialize(array)
  bucketizer  = Bucketizer.new(array)
  @buckets    = bucketizer.create_buckets
end

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



12
13
14
# File 'lib/bar_graph.rb', line 12

def buckets
  @buckets
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/bar_graph.rb', line 20

def [](key)
  return @buckets[key]
end

#bucket_contents_lengthObject



36
37
38
# File 'lib/bar_graph.rb', line 36

def bucket_contents_length
  @buckets.reduce(0) { |sum, x| sum + x[1] }
end

#each(&block) ⇒ Object



14
15
16
17
18
# File 'lib/bar_graph.rb', line 14

def each(&block)
  @buckets.each do |b|
    yield b
  end
end

#inspectObject



48
49
50
# File 'lib/bar_graph.rb', line 48

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

#lengthObject



24
25
26
# File 'lib/bar_graph.rb', line 24

def length
  return @buckets.length
end

#longest_category_nameObject



40
41
42
43
44
45
46
# File 'lib/bar_graph.rb', line 40

def longest_category_name
  longest = ''

  @buckets.each { |h| longest = h[0] if (h[0].length > longest.length) }

  return (longest == '') ? 0 : longest
end

#max_valueObject



32
33
34
# File 'lib/bar_graph.rb', line 32

def max_value
  return (@buckets.max_by { |k, v| @buckets[k] })[1]
end

#min_valueObject



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

def min_value
  return @buckets.min_by { |k, v| @buckets[k] }[1]
end