Feature #7796

Hash#keys should return a set

Added by Alexey Muranov over 3 years ago. Updated over 3 years ago.



In my opinion, (({Hash#keys})) should return an instance of (({Set})).


#1 [ruby-core:51971] Updated by Matthew Kerwin over 3 years ago

On 7 February 2013 17:52, alexeymuranov (Alexey Muranov) <> wrote:

In my opinion, (({Hash#keys})) should return an instance of (({Set})).

Is this related to an earlier request to create OrderedHash as an alias of
Hash? If that were to happen, I'd be happy if OrderedHash#keys returned an
Array, and Hash#keys returned a Set. However if Hash remains explicitly
ordered, Hash#keys should be likewise explicitly ordered (i.e. an Array).
Unless we're also adding to spec that Set is insertion-ordered.

Matthew Kerwin, B.Sc (CompSci) (Hons)

#2 [ruby-core:51976] Updated by Martin Dürst over 3 years ago

  • Status changed from Open to Feedback

For feature requests, please provide use cases and assessment on existing code, rather than "opinions".

#3 [ruby-core:51987] Updated by Alexey Muranov over 3 years ago

Matthew, in some sense it is indeed related to #5123, because both are about Set and Hash abstractions. However, it is independent, because as long as Hash is ordered, so is Set. If ever #5123 is accepted and OrderedHash is separated from Hash, there will be also OrderedSet in addition to Set.

Martin, i will think about your request, but i do not really think in "use cases". I just think that "keys" of a Hash behave as a Set and not as an Array.

#4 [ruby-core:52011] Updated by Nathan Zook over 3 years ago

The first problem for me is that Hash is part of core while Set is part of the standard lib. One cannot have core depend on lib.

The second problem is that hash#keys has returned an array for the entire history of Ruby. It is far from clear to me that breaking that assumption is a good idea.

Now, Hash#key_set, defined when you do require 'set', has possibilities, if you have a use case.

#5 [ruby-core:52015] Updated by Alexey Muranov over 3 years ago

About the core/lib issue, there is an open request about adding Set to the core: #5478.

Also available in: Atom PDF