Class: Keycounter
- Inherits:
-
Object
- Object
- Keycounter
- Defined in:
- lib/keycounter.rb
Instance Method Summary collapse
-
#keycount(key) ⇒ Object
Create / Add to instance variable.
-
#keycount_compile ⇒ Object
Compile in array with the totals of all instance variables.
-
#keycount_reader(key) ⇒ Object
Read a single key.
- #keycount_remove(key) ⇒ Object
- #keycount_reset(key) ⇒ Object
- #keycount_stats(title = "Stats:") ⇒ Object
Instance Method Details
#keycount(key) ⇒ Object
Create / Add to instance variable
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/keycounter.rb', line 15 def keycount(key) key.gsub!(" ", "_") # no spaces or case begin if !instance_variable_get("@#{key}") instance_variable_set("@#{key}", 1) else begin instance_variable_set("@#{key}", instance_variable_get("@#{key}") + 1) rescue => e puts "Cannot set instance variable of this name..." pp e end end rescue puts "Cannot get instance variable of this name..." pp e end begin return [key, instance_variable_get("@#{key}")] rescue => e pp e end end |
#keycount_compile ⇒ Object
Compile in array with the totals of all instance variables
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/keycounter.rb', line 47 def keycount_compile keycounts = Array.new # You can't really inherit this class as the other class may also contain instance variables # its not really an exact logic this class only works alone. instance_variables.each {|n| t = n.to_s.gsub("@", "") keycounts << ["#{t}", instance_variable_get("#{n}")] } return keycounts end |
#keycount_reader(key) ⇒ Object
Read a single key
40 41 42 43 44 |
# File 'lib/keycounter.rb', line 40 def keycount_reader(key) key.gsub!(" ", "_") # no spaces or case valnum = instance_variable_get("@#{key}") return valnum end |
#keycount_remove(key) ⇒ Object
68 69 70 |
# File 'lib/keycounter.rb', line 68 def keycount_remove(key) remove_instance_variable("@#{key}") end |
#keycount_reset(key) ⇒ Object
64 65 66 |
# File 'lib/keycounter.rb', line 64 def keycount_reset(key) instance_variable_set("@#{key}", 0) end |
#keycount_stats(title = "Stats:") ⇒ Object
58 59 60 61 62 |
# File 'lib/keycounter.rb', line 58 def keycount_stats(title="Stats:") puts "#{title}" stats = keycount_compile pp stats.sort_by { |h| h[1] } end |