Class: Strings::Inflection::CombinedNoun
- Inherits:
-
Object
- Object
- Strings::Inflection::CombinedNoun
- Defined in:
- lib/strings/inflection/combined_noun.rb
Constant Summary collapse
- WHITESPACE_REGEX =
/(\s)\s+/.freeze
Instance Attribute Summary collapse
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Instance Method Summary collapse
-
#+(word) ⇒ Object
Combined with another noun.
-
#initialize(words = []) ⇒ CombinedNoun
constructor
private
Create a combined noun.
-
#join_words(separator: ", ", conjunctive: "and", final_separator: nil) ⇒ String
Join a list of words into a single sentence.
-
#to_ary ⇒ Array[String]
The combined words.
Constructor Details
#initialize(words = []) ⇒ CombinedNoun
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.
Create a combined noun
13 14 15 |
# File 'lib/strings/inflection/combined_noun.rb', line 13 def initialize(words = []) @words = words end |
Instance Attribute Details
#words ⇒ Object (readonly)
Returns the value of attribute words.
8 9 10 |
# File 'lib/strings/inflection/combined_noun.rb', line 8 def words @words end |
Instance Method Details
#+(word) ⇒ Object
Combined with another noun
23 24 25 |
# File 'lib/strings/inflection/combined_noun.rb', line 23 def +(word) CombinedNoun.new(@words + [word]) end |
#join_words(separator: ", ", conjunctive: "and", final_separator: nil) ⇒ String
Join a list of words into a single sentence
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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 ⇒ Array[String]
The combined words
65 66 67 |
# File 'lib/strings/inflection/combined_noun.rb', line 65 def to_ary @words.to_ary end |