What is the difference between == and === ?
What is the difference between == and === ?
In Ruby, the difference between ==
and ===
operators is significant and pertains to how they evaluate equality.
The ==
operator is used to check for equality between two objects. It returns true
if the objects are considered equivalent, though what constitutes equivalence can vary depending on the type of the objects and how the ==
method is implemented in their respective classes. For example, for numeric values, 1 == 1.0
returns true
because the numeric value is the same, even though the types (integer and float) are different[1][2].
On the other hand, the ===
operator, often called the case equality, triple equals, or the "case subsumption" operator, has a broader use. While it can function similarly to ==
in many cases, it is specifically designed for use in case
statements to support more complex comparison logic. In Ruby, ===
is used to determine if a value falls within a "case" in a case
expression. For example, it can check if an object is an instance of a class (String === "str"
returns true
), if an object falls within a ...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào