What does the === operator do in Ruby?

Possible duplicate:
=== vs. == in Ruby

I have seen how it has been used several times recently, but cannot understand what it is doing. Can anyone illustrate how this works?

+81
ruby
Dec 17 '10 at 3:55
source share
3 answers

Like any other method in Ruby (or virtually any object-oriented language),

a === b 

means that any class of class a wants this to mean.

However, if you do not want to confuse your colleagues, the agreement is that === is a subobject operator. Basically, this is a logical operator that asks the question "If I have a box labeled a , does it make sense to put b in this box?"

Alternative wording: "If a described a set, could b be a member of this set?"

For example:

  (1..5) === 3 # => true (1..5) === 6 # => false Integer === 42 # => true Integer === 'fourtytwo' # => false /ell/ === 'Hello' # => true /ell/ === 'Foobar' # => false 

The main use for the === operator is in case expressions, since

 case foo when bar baz when quux flurb else blarf end 

translate to something (approximately), for example

 _temp = foo if bar === _temp baz elsif quux === _temp flurb else blarf end 

Note that if you want to find this operator, it is usually called the triple equality operator or the three-dimensional operator or the equality operator. I really do not like these names, because this operator has absolutely nothing to do with equality.

In particular, one would expect the equality to be symmetric: if a is equal to b , then b better to be equal to a . In addition, one would expect equality to be transitive: if a == b and b == c , then a == c . Although it is actually impossible to guarantee that in a unified mailing language such as Ruby, you should at least make an effort to preserve this property (for example, following the coerce protocol).

However, for === there is no expectation of either symmetry or transitivity. In fact, this is not very design, not symmetrical. That is why I do not like to call it something that even remotely resembles equality. This is also why I think it should have been called something else, like ~~~ or something else.

+200
Dec 17 '10 at 5:00
source share

Thanks for your editing Jacob, I was going to call you;) I will still post a couple of examples. The implementation === differs depending on the type. For example:

 (1...3) === 2 => true /test/ === "this is a test" => true case 'test' when /blah/ "Blach" when /test/ "Test" else "Fail" end => "Test" 

Stephen, checkout http://ruby-doc.org/docs/ProgrammingRuby/ ("Pickaxe"), he should be able to help you with questions like this into the future.

+5
Dec 17 '10 at 4:08
source share

In Ruby, the === operator is used to verify equality in the when statement of the case . In other languages, this is true.

As far as I know, Ruby does not have true operators, they are all methods that call expressions in LHS, passing expressions to RHS. So, indeed, you can override any "operator" that you want your classes to do whatever you need (similar to overloading an operator in C ++).

+3
Dec 17 '10 at 3:56
source share



All Articles