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 (deprecated)

Inherited From
SleepingKingStudios::Tools::Base

Deprecated. v1.3.0 Use Toolbelt.instance instead.

Returns

Back To Top

Constructor

#initialize(toolbelt: nil) => Base

Inherited From
SleepingKingStudios::Tools::Base

Parameters

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

#fetch(hsh, key, default = nil, indifferent_key: false) => Object
#fetch(hsh, key, indifferent_key: false, &default) => Object

Overloads

#fetch(hsh, key, default = nil, indifferent_key: false) => Object

Retrieves the value at the specified key.

If the value does not exist, returns the default value, or raises a KeyError if there is no default value. If the object defines a native #fetch method, delegates to the native implementation.

Parameters
  • hsh (Hash) — the hash or hash-like object.
  • key (Object) — the key to retrieve.
  • indifferent_key (true, false) — if true and the key is a String or a Symbol, tries to match both the String and Symbol equivalent. Defaults to false.
  • default (Object) — the default value.
Returns
  • (Object) — the value at the specified key.
Raises
  • (KeyError) — if the hash does not have a value at that key and there is no default value.
#fetch(hsh, key, indifferent_key: false, &default) => Object

Retrieves the value at the specified key.

If the value does not exist, returns the value of the default block, or raises a KeyError if there is no default block. If the object defines a native #fetch method, delegates to the native implementation.

Parameters
  • hsh (Hash) — the hash or hash-like object.
  • key (Object) — the key to retrieve.
  • indifferent_key (true, false) — if true and the key is a String or a Symbol, tries to match both the String and Symbol equivalent. Defaults to false.
Yields
  • generates the default value if there is no value at the key.
Yield Parameters
  • key (Object) — the requested key.
Yield Returns
  • (Object) — the default value.
Returns
  • (Object) — the value at the specified key.
Raises
  • (KeyError) — if the hash does not have a value at that key and there is no default value.

Raises

#generate_binding(hsh) => Binding

Generates a Binding with the hash values as local variables.

This method is primarily used when rendering ERB templates.

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

#toolbelt => SleepingKingStudios::Tools::Toolbelt

Also known as: tools

Inherited From
SleepingKingStudios::Tools::Base

Returns

Back To Top


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