Tools

A library of utility services and concerns.

Class: SleepingKingStudios::Tools::HashTools

Parent Namespace
SleepingKingStudios::Tools
Inherited Classes
SleepingKingStudios::Tools::Base > Object
Defined In
lib/sleeping_king_studios/tools/hash_tools.rb

Table Of Contents

Overview

Tools for working with hash-like enumerable objects.

Back To Top

Constants

HASH_METHODS

= %i[[] count each each_key each_pair].freeze

Expected methods that a Hash-like object should implement.

Back To Top

Class Methods

.instance => SleepingKingStudios::Tools::Base

Inherited From
SleepingKingStudios::Tools::Base

Returns

Back To Top

Instance Methods

#convert_keys_to_strings(hsh) => Hash

Also known as: stringify_keys

Returns a deep copy of the hash with the keys converted to strings.

Examples

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 } }

Parameters

Returns

Raises

#convert_keys_to_symbols(hsh) => Hash

Also known as: symbolize_keys

Returns a deep copy of the hash with the keys converted to symbols.

Examples

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 } }

Parameters

Returns

Raises

#deep_dup(hsh) => Hash

Creates a deep copy of the Hash.

Examples

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' }

Parameters

Returns

Raises

#deep_freeze(hsh) => Hash

Freezes the hash and performs a deep freeze on each hash key and value.

Examples

hsh = { :one => 'one', :two => 'two', :three => 'three' }
HashTools.deep_freeze hsh

hsh.frozen?
#=> true
hsh[:one].frozen?
#=> true

Parameters

Returns

Raises

#generate_binding(hsh) => Binding

Generates a Binding with the hash values as local variables.

Examples

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'

Returns

Raises

#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.

Examples

HashTools.hash?(nil)
#=> false
HashTools.hash?([])
#=> false
HashTools.hash?({})
#=> true

Parameters

Returns

#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.

Examples

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

Parameters

Returns

Raises

See Also

#mutable?(hsh) => Boolean

Returns true if the hash or any of its contents are mutable.

Parameters

Returns

Raises

See Also

Back To Top


Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools