LoginSignup
1
1

More than 5 years have passed since last update.

Active Supportのsquishが文字列中の空白削除に便利

Posted at

いつもメソッド名を忘れてしまうのでメモ。
Active Supportのsquishを使うと、文字列の先頭と末尾の空白文字が削除され、文字列中の連続した空白文字を1つにしてくれて便利です。

" foo   bar    \n   \t   boo  ".squish # => "foo bar boo"

ちなみにactivesupport-5.0.0.1では、実装の中身が正規表現が1つだけの以下のような実装変わってます。

def squish!
  gsub!(/[[:space:]]+/, ' ')
  strip!
  self
end
1
1
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
1
1