Module: Tins::HashSymbolizeKeysRecursive

Includes:
DeepTransform
Defined in:
lib/tins/hash_symbolize_keys_recursive.rb

Overview

This module provides deep symbolization of hash keys. It handles nested structures including hashes and arrays, utilizing an iterative engine to prevent stack overflows and handle circular references.

Examples:

Basic usage

hash = { "name" => "John", "address" => { "street" => "123 Main St" } }
hash.symbolize_keys_recursive
# => { name: "John", address: { street: "123 Main St" } }

Handling circular references

hash = { "name" => "John" }
hash["self"] = hash  # Circular reference
hash.symbolize_keys_recursive(circular: "[Circular Reference]")
# => { name: "John", self: "[Circular Reference]" }

Instance Method Summary collapse

Methods included from DeepTransform

#_transform_iterative, #deep_transform

Instance Method Details

#stringify_keys_recursive(circular: nil) ⇒ Hash

Deeply converts all keys in the hash (and nested hashes) to strings, using an iterative approach to avoid SystemStackError.

Examples:

hash = { name: "John", address: { city: "NYC" } }
hash.stringify_keys_recursive
# => { "name" => "John", "address" => { "city" => "NYC" } }

Parameters:

  • circular (Object) (defaults to: nil)

    The value to return for circular references.

Returns:

  • (Hash)

    A new hash with all keys converted to strings.



61
62
63
# File 'lib/tins/hash_symbolize_keys_recursive.rb', line 61

def stringify_keys_recursive(circular: nil)
  deep_transform(key: -> x { x.to_s }, circular: circular)
end

#stringify_keys_recursive!(circular: nil) ⇒ Hash

Deeply converts all keys in the hash (and nested hashes) to strings in place, using an iterative approach.

Examples:

hash = { name: "John", address: { city: "NYC" } }
hash.stringify_keys_recursive!
# => { "name" => "John", "address" => { "city" => "NYC" } }

Parameters:

  • circular (Object) (defaults to: nil)

    The value to return for circular references.

Returns:

  • (Hash)

    The same hash with all keys converted to strings.



92
93
94
# File 'lib/tins/hash_symbolize_keys_recursive.rb', line 92

def stringify_keys_recursive!(circular: nil)
  replace stringify_keys_recursive(circular: circular)
end

#symbolize_keys_recursive(circular: nil) ⇒ Hash, ...

Deeply converts all string keys in a hash (and nested hashes/arrays) to symbols using an iterative approach to avoid SystemStackError. This method does not modify the original hash.

Examples:

Basic usage

{ "name" => "John", "age" => 30 }.symbolize_keys_recursive
# => { name: "John", age: 30 }

Nested structures

{
  "user" => {
    "name" => "John",
    "hobbies" => ["reading", "swimming"]
  }
}.symbolize_keys_recursive
# => { user: { name: "John", hobbies: ["reading", "swimming"] } }

Circular reference handling

hash = { "name" => "John" }
hash["self"] = hash
hash.symbolize_keys_recursive(circular: "[Circular]")
# => { name: "John", self: "[Circular]" }

Parameters:

  • circular (Object) (defaults to: nil)

    The value to use when encountering circular references. Defaults to nil, which means circular references will be ignored.

Returns:



47
48
49
# File 'lib/tins/hash_symbolize_keys_recursive.rb', line 47

def symbolize_keys_recursive(circular: nil)
  deep_transform(key: -> x { x.to_sym }, circular: circular)
end

#symbolize_keys_recursive!(circular: nil) ⇒ Hash, ...

Deeply converts all string keys in a hash (and nested hashes/arrays) to symbols using an iterative approach. This method modifies the original hash in place.

Examples:

Basic usage

hash = { "name" => "John", "age" => 30 }
hash.symbolize_keys_recursive!
# => { name: "John", age: 30 }
# hash is now modified in place

Parameters:

  • circular (Object) (defaults to: nil)

    The value to use when encountering circular references. Defaults to nil, which means circular references will be ignored.

Returns:



78
79
80
# File 'lib/tins/hash_symbolize_keys_recursive.rb', line 78

def symbolize_keys_recursive!(circular: nil)
  replace symbolize_keys_recursive(circular: circular)
end