0
0

More than 1 year has passed since last update.

[Ruby] ifとunlessの考え方の違い

Posted at

ifとunlessの違いがわかっていませんでしたので、自分なりに纏めてみました。

#if ~ end
if A == B 
 puts C
end
#unless ~ end
unless A == B
 puts C
end

どちらもtrueで処理が実行されていくのは一緒です。
ですが、trueになる条件が違います。

①ifの場合、真がtrueとなり、処理が実行される。

#if ~ end
if A == B 
 puts C
end

②unlessの場合、偽がtrueとなり、処理が実行される。

#unless ~ end
unless A == B
 puts C
end

勘違いしやすいですが、処理される条件を理解してメソッドを使用しましょう。

 

0
0
2

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