mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Triple Equals: The Case Equality Operator as a Ruby til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1172 TILs and counting..._
|
_1173 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -982,6 +982,7 @@ _1172 TILs and counting..._
|
|||||||
- [Squeeze Out The Extra Space](ruby/squeeze-out-the-extra-space.md)
|
- [Squeeze Out The Extra Space](ruby/squeeze-out-the-extra-space.md)
|
||||||
- [String Interpolation With Instance Variables](ruby/string-interpolation-with-instance-variables.md)
|
- [String Interpolation With Instance Variables](ruby/string-interpolation-with-instance-variables.md)
|
||||||
- [Summing Collections](ruby/summing-collections.md)
|
- [Summing Collections](ruby/summing-collections.md)
|
||||||
|
- [Triple Equals: The Case Equality Operator](ruby/triple-equals-the-case-equality-operator.md)
|
||||||
- [Turn Key And Value Arrays Into A Hash](ruby/turn-key-and-values-arrays-into-a-hash.md)
|
- [Turn Key And Value Arrays Into A Hash](ruby/turn-key-and-values-arrays-into-a-hash.md)
|
||||||
- [Turning Any Class Into An Enumerator](ruby/turning-any-class-into-an-enumerator.md)
|
- [Turning Any Class Into An Enumerator](ruby/turning-any-class-into-an-enumerator.md)
|
||||||
- [Turning Things Into Hashes](ruby/turning-things-into-hashes.md)
|
- [Turning Things Into Hashes](ruby/turning-things-into-hashes.md)
|
||||||
|
|||||||
47
ruby/triple-equals-the-case-equality-operator.md
Normal file
47
ruby/triple-equals-the-case-equality-operator.md
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
# Triple Equals: The Case Equality Operator
|
||||||
|
|
||||||
|
The standard equality operator in Ruby is the double equals (`==`).
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> 2 + 2 == 4
|
||||||
|
=> true
|
||||||
|
```
|
||||||
|
|
||||||
|
Ruby supports another operator that looks sneakily like this, but with
|
||||||
|
different behavior. It's the triple equals (`===`) which is called the [case
|
||||||
|
equality
|
||||||
|
operator](https://ruby-doc.org/core-3.0.3/Object.html#method-i-3D-3D-3D) (or
|
||||||
|
case subsumption operator).
|
||||||
|
|
||||||
|
Though the specific behavior can be overridden on a class by class basis, the
|
||||||
|
operator is generally used to check if the first operand is a bucket that the
|
||||||
|
second operand fits into.
|
||||||
|
|
||||||
|
Here are some examples:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> (1..10) === 5
|
||||||
|
=> true
|
||||||
|
> (1..10) === 13
|
||||||
|
=> false
|
||||||
|
|
||||||
|
> Integer === 7
|
||||||
|
=> true
|
||||||
|
> Integer === 'nope'
|
||||||
|
=> false
|
||||||
|
|
||||||
|
> /fun/ === "fundamentals"
|
||||||
|
=> true
|
||||||
|
> /taco/ === "fundamentals"
|
||||||
|
=> false
|
||||||
|
|
||||||
|
> Object === String
|
||||||
|
=> true
|
||||||
|
> String === Object
|
||||||
|
=> false
|
||||||
|
```
|
||||||
|
|
||||||
|
It's important to understand how this works because `===` is the operator used
|
||||||
|
under the hood by Ruby's case statements.
|
||||||
|
|
||||||
|
[source](https://stackoverflow.com/questions/4467538/what-does-the-operator-do-in-ruby/4467823#4467823)
|
||||||
Reference in New Issue
Block a user