Class: Gooby::CounterHash
- Inherits:
-
GoobyObject
- Object
- GoobyObject
- Gooby::CounterHash
- Defined in:
- lib/gooby_counter_hash.rb
Overview
This class wrappers a Hash object and provides increment/decrement functionality
for a given key. It is used to sum the number of things (i.e. - xml tags) in a
collection.
Instance Method Summary collapse
-
#decrement(key) ⇒ Object
Decrement the count for the given key.
-
#increment(key) ⇒ Object
Increment the count for the given key.
- #increment_tokens(text) ⇒ Object
-
#initialize ⇒ CounterHash
constructor
A new instance of CounterHash.
- #size ⇒ Object
-
#sorted_keys ⇒ Object
Return an Array of the sorted keys.
-
#to_s ⇒ Object
Return a String containing all key=val pairs.
-
#to_xml(aligned = false) ⇒ Object
Return an XML String containing all key=val pairs, optionally aligned.
-
#value(key) ⇒ Object
Return the Integer count for the given key; zero default.
Methods included from GoobyKernel
#character_align, #default_delimiter, #invalid_altitude, #invalid_heartbeat, #invalid_latitude, #invalid_longitude, #invalid_time, #project_author, #project_copyright, #project_date, #project_embedded_comment, #project_license, #project_name, #project_version_number, #project_year, #read_as_ascii_lines, #read_lines, #strip_lines, #tokenize
Constructor Details
#initialize ⇒ CounterHash
Returns a new instance of CounterHash.
19 20 21 |
# File 'lib/gooby_counter_hash.rb', line 19 def initialize @hash = Hash.new(0) end |
Instance Method Details
#decrement(key) ⇒ Object
Decrement the count for the given key.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/gooby_counter_hash.rb', line 51 def decrement(key) if key == nil return end if (@hash.has_key?(key)) val = @hash[key] @hash[key] = val - 1 else @hash[key] = -1 end end |
#increment(key) ⇒ Object
Increment the count for the given key.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gooby_counter_hash.rb', line 33 def increment(key) if key == nil return end if (@hash.has_key?(key)) val = @hash[key] @hash[key] = val + 1 else @hash[key] = 1 end end |
#increment_tokens(text) ⇒ Object
45 46 47 48 |
# File 'lib/gooby_counter_hash.rb', line 45 def increment_tokens(text) tokens = tokenize(text) tokens.each { |token| increment(token) } end |
#size ⇒ Object
23 24 25 |
# File 'lib/gooby_counter_hash.rb', line 23 def size @hash.size end |
#sorted_keys ⇒ Object
Return an Array of the sorted keys.
64 65 66 |
# File 'lib/gooby_counter_hash.rb', line 64 def sorted_keys @hash.keys.sort end |
#to_s ⇒ Object
Return a String containing all key=val pairs.
69 70 71 72 73 74 75 76 |
# File 'lib/gooby_counter_hash.rb', line 69 def to_s s = "CHash:" sorted_keys.each { |key| val = @hash[key] s << " key: [#{key}] val: [#{val}]\n" } s end |
#to_xml(aligned = false) ⇒ Object
Return an XML String containing all key=val pairs, optionally aligned.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gooby_counter_hash.rb', line 79 def to_xml(aligned=false) s = "<CHash>" sorted_keys.each { |key| val = @hash[key] (aligned) ? s << "\n " : s << '' s << " <entry key='#{key}' value='#{val}'/>" } if aligned s << "\n " end s << " </CHash>" s end |
#value(key) ⇒ Object
Return the Integer count for the given key; zero default.
28 29 30 |
# File 'lib/gooby_counter_hash.rb', line 28 def value(key) (@hash.has_key?(key)) ? @hash[key] : 0 end |