0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

nil?,null?,empty?,present?の違いについて

Posted at

はじめに

nil、空、空文字の違いの理解がイマイチのため、整理した。

nil?

nilの場合はtrue、それ以外はfalseを返す。

empty?

空の場合はtrue、それ以外はfalseを返す。空は、""[]{}のようないれものしかない状態をさす。

blank?

nilに加えて、空、空文字に対してtrueを返す。それ以外はfalseを返す。nil?empty?の足し算。

present?

blank?の逆で、nil、空白文字列、空の文字列などの場合はfalse、そうでない場合はtrueが返る。

まとめ

以上を求めた表は以下の通り。

レシーバ nil? empty? blank? present?
nil true NoMethodError true false
[] false true true false
{} false true true false
"" false true true false
" " false false true false
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?