Feature #7796
closedHash#keys should return a set
Description
=begin
In my opinion, (({Hash#keys})) should return an instance of (({Set})).
=end
Updated by phluid61 (Matthew Kerwin) about 11 years 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)
Updated by duerst (Martin Dürst) about 11 years ago
- Status changed from Open to Feedback
For feature requests, please provide use cases and assessment on existing code, rather than "opinions".
Updated by alexeymuranov (Alexey Muranov) about 11 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.
Updated by Student (Nathan Zook) about 11 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.
Updated by alexeymuranov (Alexey Muranov) about 11 years ago
About the core/lib issue, there is an open request about adding Set to the core: #5478.