This article mentions 4 ways to invoke procs in ruby 1.9, and === is one of them. I don’t understand why this would be done this way at all. Does it have any relationship to the normal meaning of === (asking if the two objects are the same object)?
irb(main):010:0> f =-> n {[:hello, n]}
=> #
irb(main):011:0> f.call(:hello)
=> [:hello, :hello]
irb(main):012:0> f === :hello
=> [:hello, :hello]
irb(main):013:0> Object.new === Object.new
=> false
irb(main):014:0> f === f
=> [:hello, #]
This is what the docs have to say:
This is a, perhaps contrived, example:
It works because the
case/whenis basically executed like this:The
case/whenchecks which branch to execute by calling===on the arguments towhenclauses, picking the first that returns a truthy value.Despite its similarity to the equality operator (
==) it not a stronger or weaker form of it. I try to think of the===operator as the “belongs to” operator.Classdefines it so that you can check if an object belongs to the class (i.e. is an instance of the class or a subclass of the class),Rangedefines it as to check if the argument belongs to the range (i.e. is included in the range), and so on. This doesn’t really make theProccase make more sense, but think of it as a tool for making your own belongs to operators, like my example above; I defined an object that can determine if something belongs to the set of even numbers.