Class: Strings::Case::Acronyms Private
- Inherits:
-
Object
- Object
- Strings::Case::Acronyms
- Defined in:
- lib/strings/case/acronyms.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A collection of acronyms
Instance Attribute Summary collapse
-
#entries ⇒ Hash{String => String}
readonly
private
Mappings of downcased string to an acronym.
-
#pattern ⇒ Regexp
readonly
A pattern.
Class Method Summary collapse
-
.from(acronyms = []) ⇒ Strings::Case::Acronyms
Create instance from an array of acronyms.
Instance Method Summary collapse
-
#add(string) ⇒ void
(also: #<<)
Add an acronym.
-
#fetch(string) ⇒ String
Find an acronym.
-
#initialize(acronyms = []) ⇒ Acronyms
constructor
Create an Acronyms instance.
-
#to_a ⇒ Array<String>
Convert to an array of all acronyms.
Constructor Details
#initialize(acronyms = []) ⇒ Acronyms
Create an Acronyms instance
56 57 58 59 60 61 |
# File 'lib/strings/case/acronyms.rb', line 56 def initialize(acronyms = []) @entries = {} @pattern = /(?!)/ # match nothing acronyms.each { |acronym| add(acronym) } end |
Instance Attribute Details
#entries ⇒ Hash{String => String} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Mappings of downcased string to an acronym
35 36 37 |
# File 'lib/strings/case/acronyms.rb', line 35 def entries @entries end |
#pattern ⇒ Regexp (readonly)
A pattern
45 46 47 |
# File 'lib/strings/case/acronyms.rb', line 45 def pattern @pattern end |
Class Method Details
.from(acronyms = []) ⇒ Strings::Case::Acronyms
Create instance from an array of acronyms
20 21 22 23 24 |
# File 'lib/strings/case/acronyms.rb', line 20 def self.from(acronyms = []) return acronyms if acronyms.is_a?(self) new(acronyms) end |
Instance Method Details
#add(string) ⇒ void Also known as: <<
This method returns an undefined value.
Add an acronym
77 78 79 80 |
# File 'lib/strings/case/acronyms.rb', line 77 def add(string) @entries[string.downcase] = string @pattern = /#{Regexp.union(to_a)}(?=\b|[^\p{Ll}])/ end |
#fetch(string) ⇒ String
Find an acronym
94 95 96 |
# File 'lib/strings/case/acronyms.rb', line 94 def fetch(string) @entries[string.downcase] end |
#to_a ⇒ Array<String>
Convert to an array of all acronyms
106 107 108 |
# File 'lib/strings/case/acronyms.rb', line 106 def to_a @entries.values end |