Class: Support::String::CharacterCounter
- Inherits:
-
Object
- Object
- Support::String::CharacterCounter
- Defined in:
- lib/support/string/character_counter.rb
Instance Attribute Summary collapse
-
#count_hash ⇒ Object
readonly
Returns the value of attribute count_hash.
Instance Method Summary collapse
- #count(string) ⇒ Object
-
#initialize ⇒ CharacterCounter
constructor
A new instance of CharacterCounter.
Constructor Details
#initialize ⇒ CharacterCounter
Returns a new instance of CharacterCounter.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/support/string/character_counter.rb', line 8 def initialize @count_hash = { length: { count: 0 }, uppercase: characters_to_dictionary(('A'..'Z').to_a), lowercase: characters_to_dictionary(('a'..'z').to_a), number: characters_to_dictionary(('0'..'9').to_a), special: characters_to_dictionary([' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '[', ']', '{', '}', '|', '"', '/', '\\', '.', ',', '`', '<', '>', ':', ';', '?', '~', "'"]), unknown: {} } end |
Instance Attribute Details
#count_hash ⇒ Object (readonly)
Returns the value of attribute count_hash.
6 7 8 |
# File 'lib/support/string/character_counter.rb', line 6 def count_hash @count_hash end |
Instance Method Details
#count(string) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/support/string/character_counter.rb', line 19 def count(string) raise ArgumentError, "Invalid value for string: #{string}" if string.nil? string.chars.each { |c| tally_character(c) } @count_hash[:length][:count] = string.length @count_hash end |