Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/wrap.rb

Overview

Instance Method Summary collapse

Instance Method Details

#tail!(pos) ⇒ Object



38
39
40
41
# File 'lib/wrap.rb', line 38

def tail!(pos)
  self[0..pos] = ""
  strip!
end

#wrap(width = 80, hanging_indent = 0, magic_lists = false) ⇒ Object



4
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
30
31
32
33
34
35
36
# File 'lib/wrap.rb', line 4

def wrap(width = 80, hanging_indent = 0, magic_lists = false)
  lines = self.split(/\n/)

  lines.collect! do |line|

    if magic_lists 
      line =~ /^([\s\-\d\.\:]*\s)/
    else 
      line =~ /^([\s]*\s)/
    end

    indent = $1.length + hanging_indent rescue hanging_indent

    buffer = ""
    first = true

    while line.length > 0
      first ? (i, first = 0, false) : i = indent              
      pos = width - i

      if line.length > pos and line[0..pos] =~ /^(.+)\s/
        subline = $1
      else 
        subline = line[0..pos]
      end
      buffer += " " * i + subline + "\n"
      line.tail!(subline.length)
    end
    buffer[0..-2]
  end

  lines.join("\n")
end