acronym.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Conver phrase to abbreviation (not necessarily acronym!)
class Acronym
  VERSION = 1

  # Regex to find abbreviation characters
  ABBREV_MATCH = /
    (?:\b(\w)       # Any letter after a word boundary
     |
    ([A-Z])[a-z]+)  # Capital letter followed by at least one lower case
  /x

  def self.abbreviate(phrase)
    # Each match of the regex contains an abbreviation character and a nil
    # - the nil disappears by joining the two.
    phrase.scan(ABBREV_MATCH).map { |match| match.join.upcase }.join
  end
end

Comments


You're not logged in right now. Please login via GitHub to comment