Class: WordWrapper::Greedy
- Inherits:
-
WordWrapper
- Object
- WordWrapper
- WordWrapper::Greedy
- Defined in:
- lib/greedy.rb
Constant Summary
Constants inherited from WordWrapper
Infinity, OneSpaceWidth, Width
Instance Attribute Summary
Attributes inherited from WordWrapper
Instance Method Summary collapse
Methods inherited from WordWrapper
#illegal_words, #initialize, #line_cost, #total_cost
Constructor Details
This class inherits a constructor from WordWrapper
Instance Method Details
#compute_wrapping ⇒ Object
21 22 23 24 |
# File 'lib/greedy.rb', line 21 def compute_wrapping @output = wrap @cost = total_cost(@output) end |
#cost ⇒ Object
16 17 18 19 |
# File 'lib/greedy.rb', line 16 def cost compute_wrapping unless @cost @cost end |
#wrap ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/greedy.rb', line 3 def wrap words = @input.split ans = "" while words.any? line = words.shift while words.any? and (line.length + words[0].length) <= @width-1 # room for " " line << " " << words.shift end ans << line << "\n" end @output = ans end |