Issue #6847 has been updated by citizen428 (Michael Kohl).


claytrump (Clay Trump) wrote:
>  There is no "extract!" in ActiveSupport, its "slice!" you mean.

http://apidock.com/rails/Hash/extract%21

# File activesupport/lib/active_support/core_ext/hash/slice.rb, line 35
  def extract!(*keys)
    result = {}
    keys.each {|key| result[key] = delete(key) }
    result
  end
----------------------------------------
Feature #6847: Hash#extract
https://bugs.ruby-lang.org/issues/6847#change-28801

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/