A library of utility services and concerns.
Tools for working with hash-like enumerable objects.
HASH_METHODS
= %i[[] count each each_key each_pair].freeze
Expected methods that a Hash-like object should implement.
.instance => SleepingKingStudios::Tools::Base
#convert_keys_to_strings(hsh) => Hash
Also known as:
stringify_keys
Returns a deep copy of the hash with the keys converted to strings.
hsh = { :one => 1, :two => 2, :three => 3 }
cpy = HashTools.convert_keys_to_strings(hsh)
#=> { 'one' => 1, 'two' => 2, 'three' => 3 }
hsh
#=> { :one => 1, :two => 2, :three => 3 }
hsh = { :odd => { :one => 1, :three => 3 }, :even => { :two => 2, :four => 4 } }
cpy = HashTools.convert_keys_to_strings(hsh)
#=> { 'odd' => { 'one' => 1, 'three' => 3 }, 'even' => { 'two' => 2, 'four' => 4 } }
hsh
#=> { :odd => { :one => 1, :three => 3 }, :even => { :two => 2, :four => 4 } }
#convert_keys_to_symbols(hsh) => Hash
Also known as:
symbolize_keys
Returns a deep copy of the hash with the keys converted to symbols.
hsh = { 'one' => 1, 'two' => 2, 'three' => 3 }
cpy = HashTools.convert_keys_to_symbols(hsh)
#=> { :one => 1, :two => 2, :three => 3 }
hsh
#=> { 'one' => 1, 'two' => 2, 'three' => 3 }
hsh = { 'odd' => { 'one' => 1, 'three' => 3 }, 'even' => { 'two' => 2, 'four' => 4 } }
cpy = HashTools.convert_keys_to_strings(hsh)
#=> { :odd => { :one => 1, :three => 3 }, :even => { :two => 2, :four => 4 } }
hsh
#=> { 'odd' => { 'one' => 1, 'three' => 3 }, 'even' => { 'two' => 2, 'four' => 4 } }
#deep_dup(hsh) => Hash
Creates a deep copy of the Hash.
hsh = { :one => 'one', :two => 'two', :three => 'three' }
cpy = HashTools.deep_dup hsh
cpy.update :four => 'four'
#=> { :one => 'one', :two => 'two', :three => 'three', :four => 'four' }
hsh
#=> { :one => 'one', :two => 'two', :three => 'three' }
cpy[:one].sub!(/on/, 'vu'); cpy
#=> { :one => 'vun', :two => 'two', :three => 'three', :four => 'four' }
hsh
#=> { :one => 'one', :two => 'two', :three => 'three' }
#deep_freeze(hsh) => Hash
Freezes the hash and performs a deep freeze on each hash key and value.
hsh = { :one => 'one', :two => 'two', :three => 'three' }
HashTools.deep_freeze hsh
hsh.frozen?
#=> true
hsh[:one].frozen?
#=> true
#generate_binding(hsh) => Binding
Generates a Binding with the hash values as local variables.
hsh = { :one => 'one', :two => 'two', :three => 'three' }
binding = HashTools.generate_binding(hsh)
#=> Binding
binding.local_variable_defined?(:one)
#=> true
binding.local_variable_get(:one)
#=> 'one'
binding.eval('one')
#=> 'one'
#hash?(obj) => Boolean
Returns true if the object is or appears to be a Hash.
This method checks for the method signatures of the object. A Hash-like method will define all of the the #[], #count, #each, #each_key, and #each_value methods.
HashTools.hash?(nil)
#=> false
HashTools.hash?([])
#=> false
HashTools.hash?({})
#=> true
#immutable?(hsh) => Boolean
Returns true if the hash is immutable.
A hash is considered immutable if the hash itself is frozen and each key and value in the hash is immutable.
HashTools.immutable?({ :id => 0, :title => 'The Ramayana' })
#=> false
HashTools.immutable?({ :id => 0, :title => +'The Ramayana' }.freeze)
#=> false
HashTools.immutable?({ :id => 0, :title => 'The Ramayana' }.freeze)
#=> true
#mutable?(hsh) => Boolean
Returns true if the hash or any of its contents are mutable.
Back to Documentation | Versions | 1.2 | Reference | SleepingKingStudios | SleepingKingStudios::Tools