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
# File 'lib/support/string/character_counter.rb', line 8

def initialize
  @count_hash = {
    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

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)


18
19
20
21
22
23
24
# File 'lib/support/string/character_counter.rb', line 18

def count(string)
  raise ArgumentError, "Invalid value for string: #{string}" if string.nil?

  string.split('').each { |c| tally_character(c) }

  @count_hash
end