class Strings::Inflection::CombinedNoun

Constants

WHITESPACE_REGEX

Attributes

words[R]

Public Class Methods

new(words = []) click to toggle source

Create a combined noun

@api private

# File lib/strings/inflection/combined_noun.rb, line 13
def initialize(words = [])
  @words = words
end

Public Instance Methods

+(word) click to toggle source

Combined with another noun

@param [String] word

the word to combine with

@api public

# File lib/strings/inflection/combined_noun.rb, line 23
def +(word)
  CombinedNoun.new(@words + [word])
end
join_words(separator: ", ", conjunctive: "and", final_separator: nil) click to toggle source

Join a list of words into a single sentence

@example

CombinedNoun(["one", "two", "three"]).join_words
# => "one, two and three"

@param [Array] words

the words to join

@param [String] separator

the character to use to join words, defaults to `,`

@param [String] final_separator

the separator used before joining the last word

@param [String] conjunctive

the word used for combining the last word with the rest

@return [String]

@api public

# File lib/strings/inflection/combined_noun.rb, line 45
def join_words(separator: ", ", conjunctive: "and", final_separator: nil)
  oxford_comma = final_separator || separator

  case words.length
  when 0
    ""
  when 1, 2
    words.join(" #{conjunctive} ").gsub(WHITESPACE_REGEX, "\\1")
  else
    ((words[0...-1]).join(separator.to_s) +
    "#{oxford_comma} #{conjunctive} " + words[-1])
      .gsub(WHITESPACE_REGEX, "\\1").gsub(/\s*(,)/, "\\1")
  end
end
to_ary() click to toggle source

The combined words

@return [Array]

@api public

# File lib/strings/inflection/combined_noun.rb, line 65
def to_ary
  @words.to_ary
end