Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/patches/core_ext/hash/flatten_keys.rb

Instance Method Summary collapse

Instance Method Details

#flatten_keysObject

Returns a flat hash where all nested keys are collapsed into a an array of keys.

hash = { person: { name: { first: 'Rob' }, age: '28' } }
hash.flatten_keys   # => {[:person, :name, :first]=>"Rob", [:person, :age]=>"28"}
hash                # => { person: { name: { first: 'Rob' }, age: '28' } }


11
12
13
# File 'lib/patches/core_ext/hash/flatten_keys.rb', line 11

def flatten_keys
  _flatten_keys(self)
end

#flatten_keys!Object

Replaces current hash with a flat hash where all nested keys are collapsed into a an array of keys. Returns nil if no changes were made, otherwise returns the hash.

hash = { person: { name: { first: 'Rob' }, age: '28' } }
hash.flatten_keys!   # => {[:person, :name, :first]=>"Rob", [:person, :age]=>"28"}
hash                 # => {[:person, :name, :first]=>"Rob", [:person, :age]=>"28"}


21
22
23
# File 'lib/patches/core_ext/hash/flatten_keys.rb', line 21

def flatten_keys!
  replace(_flatten_keys(self))
end