Project

General

Profile

Actions

Feature #14371

closed

New option "recursive: true" for Hash#transform_keys!

Added by tagomoris (Satoshi Tagomori) almost 7 years ago. Updated almost 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:84914]

Description

Hash#transform_keys! is available when we want to symbolize hash keys.
But in some/many cases (for example, receiver hash object is nested configuration tree loaded from any files), hash object are
nested object, which has hashes or arrays of hashes as values.
It's super useful if we can transform keys of such nested values.

My proposal is Hash#transform_keys!(recursive: true, &:to_sym). Pseudo code is:

def transform_keys!(recursive: false, &block)
  # do original transform_keys! here
  values.each do |v|
    if v.respond_to?(:each)
      v.each{|i| i.transform_keys!(recursive: true, &block) if i.respond_to?(:transform_keys!) }
    else v.respond_to?(:transform_keys!)
      v.transform_keys!(recursive: true, &block)
    end
  end if recursive
end

The most major usage example is: config = MyAwesomeFormat.load(file); config.transform_keys!(recursive: true, &:to_sym).

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0