Ruby null(nil)の判定サンプル

環境
Windows11 pro 64bit
ruby ruby 3.0.3p157

構文
nil?では、対象オブジェクトがnilかどうかを確認することが可能で、nilの場合「true」それ以外は「false」が返却されます。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
str1 = ""
str2 = 0
str3 = []
str4 = nil
p str1.nil?
p str2.nil?
p str3.nil?
p str4.nil?
str1 = "" str2 = 0 str3 = [] str4 = nil p str1.nil? p str2.nil? p str3.nil? p str4.nil?
str1 = ""
str2 = 0
str3 = []
str4 = nil
 
p str1.nil?
p str2.nil?
p str3.nil?
p str4.nil?

実行結果
false
false
false
true

Ruby

Posted by arkgame