Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Javaプログラマーが学ぶRuby基礎/Rubyのunless文

概要

Rubyのunless文について説明します。

unless文

unless condition

end

であらわされ、

if !condition

end

と等価です。

サンプルコード

if文の記事で紹介したコードのif部分をunlessに変えると
結果が反転することが分かります。

nil_sample = Array.new
unless nil_sample[0].nil?
	puts(true)
else
	puts(false)
end

nil_sample[0] = 5
if nil_sample[0].nil? then
	puts(true)
else
	puts(false)
end
出力
false
true