Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/text.rb
Instance Method Summary collapse
-
#eighty ⇒ Object
Divides a string up into 80-character lines.
-
#line_divide(n) ⇒ Object
Divides a string up into n-character lines.
Instance Method Details
#eighty ⇒ Object
Divides a string up into 80-character lines.
32 33 34 |
# File 'lib/text.rb', line 32 def eighty self.line_divide(80) end |
#line_divide(n) ⇒ Object
Divides a string up into n-character lines. Pushes a word down onto the next line if it doesn’t fit, like on a page in a book.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/text.rb', line 5 def line_divide(n) answer = [] src = self.chars loop do # for each line... # remove any leading space from the line src = src.join.strip.split('') case src[n-1] # checks the nth character when ' ', '-' answer << src.shift(n).join when nil # if line < n characters answer << src.shift(n).join break else # if n+1th character is not ' ' or '-' if src[n] == ' ' answer << src.shift(n).join else # if nth character is mid-word i = (/[ -]/ =~ src[0,n].join.reverse) answer << src.shift(n - i).join end end end answer.join("\n") end |