class String
add print_multiline
method to String
class
Public Instance Methods
print_multiline(width=80, options={})
click to toggle source
# File lib/mgnu/common.rb, line 49 def print_multiline(width=80, options={}) return unless self.length > 0 indent = ' ' * (options[:indent] || 12) x = width - indent.length # string broken up with spaces or solid string split_str = self.scan(/(.{1,#{x}})(?: +|$)\n?|(.{#{x}})/) out = '' # print first line without indent out += split_str.first[0] || split_str.first[1] if split_str.length > 1 out += "\n" end # print all other lines with indent out += split_str[1..-1].map do |str, other| "#{indent}#{str || other}" end.join("\n") out end