Class: String

Inherits:
Object show all
Defined in:
lib/filigree/string.rb

Overview

Classes and Modules #

Instance Method Summary collapse

Instance Method Details

#segment(indent, max_length = 80) ⇒ Object

Chop up the string into multiple lines so that no line is longer than the specified number of characters.

Parameters:

  • indent (Fixnum)

    Indentation to put before each line; it is assumed that this indentation is already present for the first line

  • max_length (Fixnum) (defaults to: 80)

    Maximum length per line



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/filigree/string.rb', line 25

def segment(indent, max_length = 80)
	lines = Array.new

	words = self.split(/\s/)
	line  = words.shift

	line_length = indent + line.length

	words.each do |word|
		new_length = line_length + 1 + word.length

		if new_length < max_length
			line       += " #{word}"
			line_length = new_length

		else
			lines << line

			line        = word
			line_length = indent + word.length
		end
	end

	lines << line unless line.empty?

	lines.join("\n" + (' ' * indent))
end