Class: ToBarGraph::BarGraph
- Inherits:
-
Object
- Object
- ToBarGraph::BarGraph
- Includes:
- Enumerable
- Defined in:
- lib/bar_graph.rb
Instance Attribute Summary collapse
-
#buckets ⇒ Object
readonly
Returns the value of attribute buckets.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #bucket_contents_length ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(array) ⇒ BarGraph
constructor
A new instance of BarGraph.
- #inspect ⇒ Object
- #length ⇒ Object
- #longest_category_name ⇒ Object
- #max_value ⇒ Object
- #min_value ⇒ Object
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
#buckets ⇒ Object (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_length ⇒ Object
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 |
#inspect ⇒ Object
48 49 50 |
# File 'lib/bar_graph.rb', line 48 def inspect return "class: #{self.class} object_id: #{self.object_id}" end |
#length ⇒ Object
24 25 26 |
# File 'lib/bar_graph.rb', line 24 def length return @buckets.length end |
#longest_category_name ⇒ Object
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_value ⇒ Object
32 33 34 |
# File 'lib/bar_graph.rb', line 32 def max_value return (@buckets.max_by { |k, v| @buckets[k] })[1] end |
#min_value ⇒ Object
28 29 30 |
# File 'lib/bar_graph.rb', line 28 def min_value return @buckets.min_by { |k, v| @buckets[k] }[1] end |