string_indent.rb
class String
def indent(level = 1, count = 2)
spaces = ' ' * count * level
each_line.map {|line| line.chomp.empty? ? line : spaces + line }.join
end
end
<<CONFIG.indent
MyApp.configure do |app|
# ...
end
CONFIG
# MyApp.configure do |app|
# # ...
# end
line.chomp.empty?
で空行の場合には空白を追加しないようにしているのがミソ。