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?

More than 3 years have passed since last update.

【初心者必読】Ruby の誤解あるあるAdvent Calendar 2020

Day 10

Ruby の && や || は条件演算子じゃない

Posted at

Ruby の初心者向け記事で &&|| を「条件演算子」と書いているものをときどき目にします。
おそらく条件式で使うのでそう呼んでしまっているのだろうと思いますが,これらは「論理演算子」です。

一方,条件演算子というのは,

(1..9).each do |n|
  puts "#{ n } is #{ n.odd? ? "odd" : "even" }"
end

に出てくる式

n.odd? ? "odd" : "even"

? : という二つの記号で表される演算子のことです。
三つの項を取るので「三項演算子」とも呼ばれます(Ruby では条件演算子以外に三つの項を取る演算子はありません)。
演算子式 (Ruby 3.0.0 リファレンスマニュアル)

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?