6 # Constantizes a string, changing it from snake_case or 'space case' to
7 # TitleCase, however necessary
10 str = str.gsub /\s/, '_'
11 str = str.gsub(/(^|_)(\w)/) { "#{$2.capitalize}" }
15 File.join(self, o.to_s)
19 if spaces.respond_to? :to_s # duck,
20 self.split("\n").map {|s| [spaces, s].join }.join("\n")
21 elsif spaces.respond_to? :to_i # duck,
22 self.split("\n").map {|s| [(' ' * spaces), s].join }.join("\n")
24 raise ArgumentError, "#{spaces} is neither string-ish nor numeric-ish"
28 # Ruby 1.8's #length doesn't like multibyte Unicode. Thanks Mikael Høilund!
33 # Simply returns an array of two string pieces split at +length+.
35 self.scan /.{1,#{length}}/
38 # Wraps a string, *intelligently*
39 def wrap width, min = nil
40 raise ArgumentError, "#{width} is not numeric-ish" unless width.respond_to? :to_i
41 min ||= (width.to_i * 0.75).to_i # Default to about a third of the full width
42 raise ArgumentError, "#{min} is not numeric-ish" unless min.respond_to? :to_i
45 self.inject([""]) do |wrapped, word|
46 #puts "word: #{word.inspect}, current line: #{wrapped.last.inspect}"
47 # If we're still short enough to fit the word, do so
48 if wrapped.last.length + word.rstrip.length <= width
49 #puts "- new length #{wrapped.last.length + word.rstrip.length} (#{wrapped.last.length} + #{word.rstrip.length}) is less than #{width}\n\n"
51 # Else, if we're less than minimum width
52 elsif wrapped.last.length < min
53 #puts "- new length #{wrapped.last.length + word.rstrip.length} (#{wrapped.last.length} + #{word.rstrip.length}) would be more than #{width}"
54 #puts "- current length #{wrapped.last.length} is less than #{min}\n\n"
55 bits = word.split_at(width - wrapped.last.length)
56 wrapped.last << bits.shift
57 bits.join.split_at(width)
58 bits.each {|bit| wrapped << bit}
59 # Else if neither can fit on current line, nor is line short enough; and
60 # the word is short enough to fit on the new line
61 elsif word.chomp.length < width
62 #puts "- new length #{wrapped.last.length + word.rstrip.length} (#{wrapped.last.length} + #{word.rstrip.length}) would be more than #{width}"
63 #puts "- current length #{wrapped.last.length} is more than #{min}"
64 #puts "- word's length #{word.chomp.length} is less than #{width}\n\n"
66 # If it can't fit on the current line, and it can't fit wholly on a line
69 #puts "- new length #{wrapped.last.length + word.rstrip.length} (#{wrapped.last.length} + #{word.rstrip.length}) would be more than #{width}"
70 #puts "- current length #{wrapped.last.length} is more than #{min}"
71 #puts "- word's length #{word.chomp.length} is more than #{width}"
72 bits = word.split_at(width)
73 bits.each {|bit| wrapped << bit}