Class: Strings::Case::Acronyms Private

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(acronyms = []) ⇒ Acronyms

Create an Acronyms instance

Examples:

acronyms = Strings::Case::Acronyms.new(%w[HTTP XML])

Parameters:

  • acronyms (Array<String>) (defaults to: [])

    an array of acronyms



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

#entriesHash{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

Examples:

acronyms.entries
# => {"http" => "HTTP"}

Returns:

  • (Hash{String => String})


35
36
37
# File 'lib/strings/case/acronyms.rb', line 35

def entries
  @entries
end

#patternRegexp (readonly)

A pattern

Examples:

acronyms.pattern

Returns:

  • (Regexp)


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

Examples:

acronyms = Strings::Case::Acronyms.new(%w[HTTP XML])

Parameters:

Returns:



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

Examples:

acronyms.add("HTTP")
acronyms << "HTTP"

Parameters:

  • string (String)

    the string name to add to the acronyms



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

Examples:

acronyms.fetch("http")

Parameters:

  • string (String)

    the string to search for an acronym

Returns:

  • (String)


94
95
96
# File 'lib/strings/case/acronyms.rb', line 94

def fetch(string)
  @entries[string.downcase]
end

#to_aArray<String>

Convert to an array of all acronyms

Examples:

acronyms.to_a

Returns:

  • (Array<String>)


106
107
108
# File 'lib/strings/case/acronyms.rb', line 106

def to_a
  @entries.values
end