Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/MrMurano/hash.rb
Direct Known Subclasses
OptionParser::CompletingHash, OptionParser::OptionMap, OrderedHash
Class Method Summary collapse
-
.flat_hash(hash, long_key = [], flat = {}) ⇒ Object
h = { :a => { :b => { :c => 1, :d => 2 }, :e => 3 }, :f => 4 } flat_hash(h) #=> :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4.
-
.nested_keys(obj) ⇒ Object
Get all hash and nested hash keys.
- .transform_keys_to_strings(value) ⇒ Object
- .transform_keys_to_symbols(value) ⇒ Object
Instance Method Summary collapse
-
#deep_merge!(other_hash, &block) ⇒ Object
From Rails.
-
#deep_symbolize_keys ⇒ Object
Returns a new hash with all keys converted to symbols, as long as they respond to
to_sym. -
#deep_symbolize_keys! ⇒ Object
Destructively converts all keys to symbols, as long as they respond to
to_sym. -
#deep_transform_keys(&block) ⇒ Object
Returns a new hash with all keys converted by the block operation.
-
#deep_transform_keys!(&block) ⇒ Object
Destructively converts all keys by using the block operation.
Class Method Details
.flat_hash(hash, long_key = [], flat = {}) ⇒ Object
h = { :a => { :b => { :c => 1, :d => 2 }, :e => 3 }, :f => 4 } flat_hash(h) #=> :b, :c]=>1, [:a, :b, :d]=>2, [:a, :e]=>3, [:f]=>4
48 49 50 51 52 |
# File 'lib/MrMurano/hash.rb', line 48 def self.flat_hash(hash, long_key=[], flat={}) return flat.update(long_key => hash) unless hash.is_a? Hash hash.each { |key, val| flat_hash(val, long_key + [key], flat) } flat end |
.nested_keys(obj) ⇒ Object
Get all hash and nested hash keys.
55 56 57 58 59 60 |
# File 'lib/MrMurano/hash.rb', line 55 def self.nested_keys(obj) obj.each_with_object([]) do |(key, val), keys| keys << key keys.concat(nested_keys(val)) if val.is_a? Hash end end |
.transform_keys_to_strings(value) ⇒ Object
From:
http://www.any-where.de/blog/ruby-hash-convert-string-keys-to-symbols/
Take keys of hash and transform those to strings.
25 26 27 28 29 |
# File 'lib/MrMurano/hash.rb', line 25 def self.transform_keys_to_strings(value) return value.map { |v| Hash.transform_keys_to_strings(v) } if value.is_a?(Array) return value unless value.is_a?(Hash) value.inject({}) { |memo, (k, v)| memo[k.to_s] = Hash.transform_keys_to_strings(v); memo } end |
.transform_keys_to_symbols(value) ⇒ Object
From:
http://www.any-where.de/blog/ruby-hash-convert-string-keys-to-symbols/
Take keys of hash and transform those to symbols.
15 16 17 18 19 |
# File 'lib/MrMurano/hash.rb', line 15 def self.transform_keys_to_symbols(value) return value.map { |v| Hash.transform_keys_to_symbols(v) } if value.is_a?(Array) return value unless value.is_a?(Hash) value.inject({}) { |memo, (k, v)| memo[k.to_sym] = Hash.transform_keys_to_symbols(v); memo } end |
Instance Method Details
#deep_merge!(other_hash, &block) ⇒ Object
From Rails.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/MrMurano/hash.rb', line 32 def deep_merge!(other_hash, &block) other_hash.each_pair do |current_key, other_value| this_value = self[current_key] if this_value.is_a?(Hash) && other_value.is_a?(Hash) self[current_key] = this_value.deep_merge(other_value, &block) elsif block_given? && key?(current_key) self[current_key] = yield(current_key, this_value, other_value) else self[current_key] = other_value end end self end |
#deep_symbolize_keys ⇒ Object
Returns a new hash with all keys converted to symbols, as long as they respond to to_sym. This includes the keys from the root hash and from all nested hashes and arrays.
hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
hash.deep_symbolize_keys
# => {:person=>{:name=>"Rob", :age=>"28"}}
94 95 96 97 |
# File 'lib/MrMurano/hash.rb', line 94 def deep_symbolize_keys # rubocop:disable Style/RescueModifier deep_transform_keys { |key| key.to_sym rescue key } end |
#deep_symbolize_keys! ⇒ Object
Destructively converts all keys to symbols, as long as they respond to to_sym. This includes the keys from the root hash and from all nested hashes and arrays.
102 103 104 |
# File 'lib/MrMurano/hash.rb', line 102 def deep_symbolize_keys! deep_transform_keys! { |key| key.to_sym rescue key } end |
#deep_transform_keys(&block) ⇒ Object
Returns a new hash with all keys converted by the block operation. This includes the keys from the root hash and from all nested hashes and arrays.
hash = { person: { name: 'Rob', age: '28' } }
hash.deep_transform_keys{ |key| key.to_s.upcase }
# => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
75 76 77 |
# File 'lib/MrMurano/hash.rb', line 75 def deep_transform_keys(&block) _deep_transform_keys_in_object(self, &block) end |
#deep_transform_keys!(&block) ⇒ Object
Destructively converts all keys by using the block operation. This includes the keys from the root hash and from all nested hashes and arrays.
82 83 84 |
# File 'lib/MrMurano/hash.rb', line 82 def deep_transform_keys!(&block) _deep_transform_keys_in_object!(self, &block) end |