lib/vedeu/support/sentence.rb in vedeu-0.3.0 vs lib/vedeu/support/sentence.rb in vedeu-0.3.1

- old
+ new

@@ -1,21 +1,34 @@ module Vedeu + # Take a collection of words (elements) and form a sentence from them. + # + # @example + # elements = ['Hydrogen', 'Helium', 'Lithium'] + # Vedeu::Sentence.construct(elements) # => 'Hydrogen, Helium and Lithium' + # class Sentence class << self + # @param elements [Array] + # @param label [String] + # @return [String] def construct(elements, label = 'elements') new(elements, label).construct end - end # Sentence eigenclass + end + # @param elements [Array] + # @param label [String] + # @return [Vedeu::Sentence] def initialize(elements, label) @elements, @label = elements, label end + # @return [String] def construct if one? first elsif two? @@ -32,33 +45,40 @@ private attr_reader :elements, :label + # @return [Boolean] def one? count == 1 end + # @return [Boolean] def two? count == 2 end + # @return [Boolean] def many? count > 2 end + # @return [String] def but_last elements[0...-1].join(', ') end + # @return [String] def first elements.first end + # @return [String] def last elements[-1] end + # @return [Fixnum] def count elements.size end end # Sentence