Class: Vedeu::Sentence

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/support/sentence.rb

Overview

Take a collection of words (elements) and form a sentence from them.

Examples:

elements = ['Hydrogen', 'Helium', 'Lithium']
Vedeu::Sentence.construct(elements) # => 'Hydrogen, Helium and Lithium'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements, label) ⇒ Vedeu::Sentence

Parameters:

  • elements (Array)
  • label (String)


21
22
23
# File 'lib/vedeu/support/sentence.rb', line 21

def initialize(elements, label)
  @elements, @label = elements, label
end

Instance Attribute Details

#elementsObject (readonly, private)

Returns the value of attribute elements.



44
45
46
# File 'lib/vedeu/support/sentence.rb', line 44

def elements
  @elements
end

#labelObject (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

Parameters:

  • elements (Array)
  • label (String) (defaults to: 'elements')

Returns:

  • (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_lastString (private)

Returns:

  • (String)


62
63
64
# File 'lib/vedeu/support/sentence.rb', line 62

def but_last
  elements[0...-1].join(', ')
end

#constructString

Returns:

  • (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

#countFixnum (private)

Returns:



77
78
79
# File 'lib/vedeu/support/sentence.rb', line 77

def count
  elements.size
end

#firstString (private)

Returns:

  • (String)


67
68
69
# File 'lib/vedeu/support/sentence.rb', line 67

def first
  elements.first
end

#lastString (private)

Returns:

  • (String)


72
73
74
# File 'lib/vedeu/support/sentence.rb', line 72

def last
  elements[-1]
end

#many?Boolean (private)

Returns:

  • (Boolean)


57
58
59
# File 'lib/vedeu/support/sentence.rb', line 57

def many?
  count > 2
end

#one?Boolean (private)

Returns:

  • (Boolean)


47
48
49
# File 'lib/vedeu/support/sentence.rb', line 47

def one?
  count == 1
end

#two?Boolean (private)

Returns:

  • (Boolean)


52
53
54
# File 'lib/vedeu/support/sentence.rb', line 52

def two?
  count == 2
end