Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/acronyms.rb

Overview

Extending base String class with ‘.acronyms`

Instance Method Summary collapse

Instance Method Details

#acronymsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/acronyms.rb', line 6

def acronyms
  return [] if empty?
  split = self.split(/[\s\-]/)

  romans = %w(I II III IV V VI VII VIII IX)

  # If a word is a roman numeral, we keep it, othewise we take the first
  # letter
  acronym = split.map do |word|
    romans.index(word).nil? ? word[0] : word
  end
  acronym = acronym.join('')

  # If a word is a roman numeral, we convert it, otherwise we take the first
  # letter
  acronym_roman = split.map do |word|
    roman_index = romans.index(word)
    !roman_index.nil? ? roman_index + 1 : word.upcase[0]
  end
  acronym_roman = acronym_roman.join('')

  [acronym, acronym_roman].uniq
end