Issue #6847 has been updated by trans (Thomas Sawyer).


It important to understand the differences and to determine the best definition. ActiveSupport's #slice returns the same type of class, which I think is important, where as #extract! always returns a Hash. Also, #slice and #slice! are provided, where as there is only #extract! in ActiveSupport. Certainly we want both. ActiveSupport's #slice also has a special feature for #convert_key, which I think that would be nice for Ruby to have too, but it is not part of Ruby at this time. So that would obviously be omitted.

I concur with Clay that #slice is the better name b/c Array#[] and Array#slice are pretty much the same, and though it might be a little bit more different that general relationship should also hold for Hash.

----------------------------------------
Feature #6847: Hash#extract
https://bugs.ruby-lang.org/issues/6847#change-28802

Author: citizen428 (Michael Kohl)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 


I often need a method to extract "subhashes" from hashes. I often monkey-patched this into hash and found "extract" to be a good name for the method. Afterwards I found out that there actually is a mutating method called "extract!" in ActiveSupport, so I guess the name is kinda established. Here's the Ruby version from my private extension library, if there's any interest in this I would try to re-implement it in C:

class Hash
  def extract(*ks)
    existing = keys & ks
    Hash[existing.zip(values_at(*existing))]
  end
end


-- 
http://bugs.ruby-lang.org/