Feature #13801

Implement case equality test for Set#===

Added by davidarnold (David Arnold) about 1 month ago. Updated 4 days ago.

Target version:


In keeping with other class semantics, Set should implement an inclusion / membership test for #===.

For example with Range:

(1..3) === 2 #=> true

Desired behavior:

Set[1, 2, 3] === 2 #=> true

Current behavior:

Set[1, 2, 3] === 2 #=> false

Associated revisions

Revision 59966
Added by knu (Akinori MUSHA) 4 days ago

Alias Set#=== to #include?

  • set.rb (Set#===): Added via [Feature #13801] by davidarnold.

Closes #1673.


#2 [ruby-core:82347] Updated by knu (Akinori MUSHA) about 1 month ago

  • Assignee set to knu (Akinori MUSHA)

#3 [ruby-core:82468] Updated by kernigh (George Koehler) 30 days ago

Here's a counterexample with Array:

[1, 2, 3] === 2 #=> false

Array#=== doesn't look inside the array. I expect Set to act almost like Array, so it would be weird if Set#=== looked inside the container but Array#=== didn't.

#4 [ruby-core:82478] Updated by davidarnold (David Arnold) 29 days ago

I expect Set to act almost like Array [...]

But why? Aside from both being collections, there is no connection. Ruby's Set isn't even implemented with an Array.

The core mathematical definition of Set is based on element membership[1]. All of the characteristic operations on sets (intersection, union, difference, etc) are derived based on element membership. When #=== has the common semantics in Ruby of membership, why should the one class, which is probably most closely tied to the notion of membership, not implement this operator?

FWIW, I would not be opposed to Array implementing #=== as a membership test either, but this is not a feature request for Array.

  1. "Set theory begins with a fundamental binary relation between an object o and a set A. If o is a member (or element) of A, the notation o ∈ A is used."

#5 Updated by knu (Akinori MUSHA) 4 days ago

  • Status changed from Open to Closed

Applied in changeset trunk|r59966.

Alias Set#=== to #include?

  • set.rb (Set#===): Added via [Feature #13801] by davidarnold.

Closes #1673.

#6 [ruby-core:82867] Updated by knu (Akinori MUSHA) 4 days ago

I don't remember but I guess I didn't define Set#=== probably because Array didn't, but I agree Set#=== could be useful and intuitive, so I've merged your PR with a slight modification (alias to save an extra method call) and a test in addition to the specs. Thanks!

Also available in: Atom PDF