LoginSignup
0
0

More than 3 years have passed since last update.

railsのArrayでnilと空白文字列の配列を除去して連結して表示する

Posted at

色々なメソッドの結果を1つの文字列として出したかったが、メソッドの返り値には nil のケースと "" のケースの2種類が存在していた。
railsのArrayの #compactnil しか省いていくれない。
reject を使って省いた。

pry(main)> ["メソッドA", nil, "", "メソッドB", "メソッドC"].reject { |e| e.to_s.empty? }.join('、')
=> "メソッドA、メソッドB、メソッドC"
0
0
2

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
0
0