[ruby-core:121587] [Ruby Feature#16993] Sets: from hash keys using Hash#key_set
From:
"Dan0042 (Daniel DeLorme) via ruby-core" <ruby-core@...>
Date:
2025-04-08 13:00:32 UTC
List:
ruby-core #121587
Issue #16993 has been updated by Dan0042 (Daniel DeLorme).
mame (Yusuke Endoh) wrote in #note-9:
> Given the convention of deriving methods such as `key_set` from `keys`, where the former returns a set instead of an array, one might expect a corresponding derivation like `Kernel#instance_variable_set` for `Kernel#instance_variables`. Unfortunately, this would conflict with an existing method name.
In https://github.com/ruby/set/pull/40 the name was changed to #keys_to_set, so this comment doesn't apply anymore.
Then again I wish all optimizations like this could be implemented at the VM level, so that `hash.keys.to_set` would automatically be efficient rather than having to explicitly use the efficient version #keys_to_set.
----------------------------------------
Feature #16993: Sets: from hash keys using Hash#key_set
https://bugs.ruby-lang.org/issues/16993#change-112642
* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
----------------------------------------
To create a set from hash keys currently implies a temporary array for all keys, rehashing all those keys and rebuilding a hash. Instead, the hash could be copied and its values set to `true`.
```ruby
h = {a: 1}
# Now:
Set.new(h.keys) # => Set[:a]
# After
h.key_set # => Set[:a], efficiently.
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/