Feature #7796

Hash#keys should return a set

Added by Alexey Muranov about 1 year ago. Updated about 1 year ago.

[ruby-core:51969]
Status:Feedback
Priority:Normal
Assignee:-
Category:core
Target version:Next Major

Description

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

History

#1 Updated by Matthew Kerwin about 1 year ago

On 7 February 2013 17:52, alexeymuranov (Alexey Muranov) <
redmine@ruby-lang.org> wrote:

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

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 Updated by Martin Dürst about 1 year ago

  • Status changed from Open to Feedback

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

#3 Updated by Alexey Muranov about 1 year 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 Updated by Nathan Zook about 1 year 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 Updated by Alexey Muranov about 1 year ago

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

Also available in: Atom PDF