LoginSignup
105
83

More than 5 years have passed since last update.

rubyの配列からnilと空文字を取り除く

Last updated at Posted at 2014-10-24
['a', nil, '', 'b'].compact.reject(&:empty?)
=> ["a", "b"]

['a', nil, '', 'b'].compact.delete_if(&:empty?)
=> ["a", "b"]

なにかで便利メソッドが提供されてる気もする...


ActiveSupportにString#blank?があった。

['a', nil, '', 'b'].reject(&:blank?)
=> ["a", "b"]
105
83
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
105
83