Module: Shoryuken::Helpers::HashUtils

Defined in:
lib/shoryuken/helpers/hash_utils.rb

Overview

Utility methods for hash manipulation.

This module provides helper methods for common hash operations that were previously implemented as core class extensions. By using a dedicated helper module, we avoid polluting the global namespace while maintaining the same functionality.

Examples:

Basic usage

hash = { 'key1' => 'value1', 'key2' => { 'nested' => 'value2' } }
symbolized = Shoryuken::Helpers::HashUtils.deep_symbolize_keys(hash)
# => { key1: 'value1', key2: { nested: 'value2' } }

Class Method Summary collapse

Class Method Details

.deep_symbolize_keys(hash) ⇒ Hash, Object

Recursively converts hash keys to symbols.

This method traverses a hash structure and converts all string keys to symbols, including nested hashes. Non-hash values are left unchanged. This is useful for normalizing configuration data loaded from YAML files.

Examples:

Converting a simple hash

hash = { 'key1' => 'value1', 'key2' => 'value2' }
HashUtils.deep_symbolize_keys(hash)
# => { key1: 'value1', key2: 'value2' }

Converting a nested hash

hash = { 'config' => { 'timeout' => 30, 'retries' => 3 } }
HashUtils.deep_symbolize_keys(hash)
# => { config: { timeout: 30, retries: 3 } }

Handling non-hash input gracefully

HashUtils.deep_symbolize_keys('not a hash')
# => 'not a hash'

Mixed value types

hash = { 'string' => 'value', 'number' => 42, 'nested' => { 'bool' => true } }
HashUtils.deep_symbolize_keys(hash)
# => { string: 'value', number: 42, nested: { bool: true } }

Parameters:

  • hash (Hash, Object)

    The hash to convert, or any other object

Returns:

  • (Hash, Object)

    Hash with symbolized keys, or the original object if not a hash



45
46
47
48
49
50
51
52
# File 'lib/shoryuken/helpers/hash_utils.rb', line 45

def deep_symbolize_keys(hash)
  return hash unless hash.is_a?(Hash)

  hash.each_with_object({}) do |(key, value), result|
    symbol_key = key.is_a?(String) ? key.to_sym : key
    result[symbol_key] = value.is_a?(Hash) ? deep_symbolize_keys(value) : value
  end
end