Class: Support::String::CharacterCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/support/string/character_counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCharacterCounter

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_hashObject (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

Raises:

  • (ArgumentError)


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