LoginSignup
3
3

More than 5 years have passed since last update.

文字列をインデントするメソッド

Posted at
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? で空行の場合には空白を追加しないようにしているのがミソ。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3