Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Designed for building a 3 letter acronym from a list of phrases
Ruby
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
MIT-LICENSE
README.md
acronym.gemspec
gem-public_cert.pem

README.md

Introducing the the acronym gem

a = ['apples in june', 'apples in july', 'oranges in august', 'october pineapple', 'grapes']
Acronym.new(a).to_a
#=> ["aij", "aik", "oia", "opi", "gra"]

a = ["apples in late june", "apples in july", "oranges in august", "october pineapple", "grapes"]
Acronym.new(a).to_a
#=> ["ailj", "aij", "oia", "opi", "gra"]

The Acronym gem accepts a list of phrases consisting of ideally 3 words or less and converts them into unique acronyms.

Here is the Array#acronyms method:

class Array

  def acronyms()
    acronym = lambda {|long_string|
      long_string.gsub!(/_/,' '); a = long_string.scan(/(\b\w)/).join
      a = (a + long_string[/\B\w+$/]).slice(0,3) if a.length <= 2
      a
    }

    self.inject([]) do |r,s|
      acronym_id = acronym.call(s.clone.downcase)
      acronym_id.succ! while r.include? acronym_id
      r << acronym_id
    end
  end

end

['flowers in March', 'flowers in May', 'summer wine'].acronyms
#=> ["fim", "fin", "swi"]
Something went wrong with that request. Please try again.