Class: Vedeu::Sentence
- Inherits:
-
Object
- Object
- Vedeu::Sentence
- Defined in:
- lib/vedeu/support/sentence.rb
Overview
Take a collection of words (elements) and form a sentence from them.
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
private
Returns the value of attribute elements.
-
#label ⇒ Object
readonly
private
Returns the value of attribute label.
Class Method Summary collapse
Instance Method Summary collapse
- #but_last ⇒ String private
- #construct ⇒ String
- #count ⇒ Fixnum private
- #first ⇒ String private
- #initialize(elements, label) ⇒ Vedeu::Sentence constructor
- #last ⇒ String private
- #many? ⇒ Boolean private
- #one? ⇒ Boolean private
- #two? ⇒ Boolean private
Constructor Details
#initialize(elements, label) ⇒ Vedeu::Sentence
21 22 23 |
# File 'lib/vedeu/support/sentence.rb', line 21 def initialize(elements, label) @elements, @label = elements, label end |
Instance Attribute Details
#elements ⇒ Object (readonly, private)
Returns the value of attribute elements.
44 45 46 |
# File 'lib/vedeu/support/sentence.rb', line 44 def elements @elements end |
#label ⇒ Object (readonly, private)
Returns the value of attribute label.
44 45 46 |
# File 'lib/vedeu/support/sentence.rb', line 44 def label @label end |
Class Method Details
.construct(elements, label = 'elements') ⇒ String
14 15 16 |
# File 'lib/vedeu/support/sentence.rb', line 14 def self.construct(elements, label = 'elements') new(elements, label).construct end |
Instance Method Details
#but_last ⇒ String (private)
62 63 64 |
# File 'lib/vedeu/support/sentence.rb', line 62 def but_last elements[0...-1].join(', ') end |
#construct ⇒ String
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vedeu/support/sentence.rb', line 26 def construct if one? first elsif two? elements.join(' and ') elsif many? [but_last, last].join(' and ') else "No #{label} have been assigned." end end |
#count ⇒ Fixnum (private)
77 78 79 |
# File 'lib/vedeu/support/sentence.rb', line 77 def count elements.size end |
#first ⇒ String (private)
67 68 69 |
# File 'lib/vedeu/support/sentence.rb', line 67 def first elements.first end |
#last ⇒ String (private)
72 73 74 |
# File 'lib/vedeu/support/sentence.rb', line 72 def last elements[-1] end |
#many? ⇒ Boolean (private)
57 58 59 |
# File 'lib/vedeu/support/sentence.rb', line 57 def many? count > 2 end |
#one? ⇒ Boolean (private)
47 48 49 |
# File 'lib/vedeu/support/sentence.rb', line 47 def one? count == 1 end |
#two? ⇒ Boolean (private)
52 53 54 |
# File 'lib/vedeu/support/sentence.rb', line 52 def two? count == 2 end |