Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

ActiveSupport | String#strip_heredoc

概要

String#strip_heredoc

詳細

String#strip_heredoc について

String#strip_heredoc

ヒアドキュメントをインデントの開始位置をインデントの1階層目として
不要な空白を除去してくれる。

ヒアドキュメントはこういう風に書けたらいいのに、と思っていたので
このアイデアはすばらしいと関心しました。

サンプル

# encoding: utf-8
require 'active_support/core_ext/string/strip'

def get_hoge
  hoge = <<-EOS.strip_heredoc
  ほげ1
    ひげ1
      はげ1
      はげ2
    ひげ2
  ほげ2
  EOS
end

puts get_hoge

出力

ほげ1
  ひげ1
    はげ1
    はげ2
  ひげ2
ほげ2