12
7

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 の empty? の逆は any? ではない

Posted at

ruby の empty? は要素の数が 0 の場合に true を返すメソッドですが、any? は真となる要素が存在している場合に true を返すメソッドなので、例えば次のコードは両方とも false を返します。

a = [false]

p a.empty? #=> false
p a.any?   #=> false

なので、 empty? の逆の結果が欲しい場合は ! を使いましょう。

a = [false]

p a.empty?   #=> false
p !a.empty?  #=> true
p a.empty?.! #=> true (これはあまり見ないかも)
12
7
3

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?