Class: SumSum
- Inherits:
-
Hash
- Object
- Hash
- SumSum
- Defined in:
- lib/sum_sum.rb,
lib/sum_sum/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #add(hash, increase_by = 1) ⇒ Object
- #bottom? ⇒ Boolean
-
#initialize(*args) ⇒ SumSum
constructor
A new instance of SumSum.
- #inspect ⇒ Object
- #root? ⇒ Boolean
- #share ⇒ Object
- #sort! ⇒ Object
Constructor Details
#initialize(*args) ⇒ SumSum
Returns a new instance of SumSum.
2 3 4 5 6 7 8 |
# File 'lib/sum_sum.rb', line 2 def initialize(*args) @parent = args.pop if args[-1].is_a?(self.class) @name = args.shift @args = args.compact.dup @count = 0 super() end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
10 11 12 |
# File 'lib/sum_sum.rb', line 10 def args @args end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
10 11 12 |
# File 'lib/sum_sum.rb', line 10 def count @count end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/sum_sum.rb', line 10 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
10 11 12 |
# File 'lib/sum_sum.rb', line 10 def parent @parent end |
Instance Method Details
#add(hash, increase_by = 1) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/sum_sum.rb', line 12 def add(hash, increase_by=1) key = hash[name] @count = @count + increase_by unless bottom? self[key] ||= SumSum.new(*args, self) self[key].add(hash, increase_by) end self end |
#bottom? ⇒ Boolean
40 41 42 |
# File 'lib/sum_sum.rb', line 40 def bottom? name.nil? end |
#inspect ⇒ Object
44 45 46 |
# File 'lib/sum_sum.rb', line 44 def inspect bottom? ? "#{count}" : "{#{name}:#{count} #{super.gsub(/^\{|\}$/, "")}}" end |
#root? ⇒ Boolean
36 37 38 |
# File 'lib/sum_sum.rb', line 36 def root? parent.nil? end |
#share ⇒ Object
22 23 24 |
# File 'lib/sum_sum.rb', line 22 def share root? ? 1.0 : count/parent.count.to_f end |
#sort! ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/sum_sum.rb', line 26 def sort! values.each(&:sort!) unless bottom? to_a.tap do |array| array.reverse!(&:count) clear array.each{|k, v| self[k] = v } end self end |