class Support::String::CharacterCounter
Attributes
count_hash[R]
Public Class Methods
new()
click to toggle source
# 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(%w(! @ # $ % ^ & * ( ) _ + - = [ ] { } | ')), unknown: {} } end
Public Instance Methods
count(string)
click to toggle source
# File lib/support/string/character_counter.rb, line 19 def count(string) raise ArgumentError, "Invalid value for string: #{string}" if string.nil? string.split('').each { |c| tally_character(c) } @count_hash[:length][:count] = string.length @count_hash end
Private Instance Methods
character_in_dictionary?(character, dictionary)
click to toggle source
# File lib/support/string/character_counter.rb, line 50 def character_in_dictionary?(character, dictionary) dictionary.key?(character) end
characters_to_dictionary(array)
click to toggle source
# File lib/support/string/character_counter.rb, line 30 def characters_to_dictionary(array) dictionary = {} array.each { |c| dictionary.store(c, 0) } dictionary end
tally_character(character)
click to toggle source
# File lib/support/string/character_counter.rb, line 37 def tally_character(character) %i(uppercase lowercase number special unknown).each do |type| if @count_hash[type].key?(character) @count_hash[type][character] += 1 return @count_hash[type][character] end end # must be new unknown char @count_hash[:unknown][character] = 1 @count_hash[:unknown][character] end