107
84

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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"]
107
84
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
107
84

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?