Module: OpenStruct::FromHash

Included in:
OpenStruct
Defined in:
lib/llm/core_ext/ostruct.rb

Instance Method Summary collapse

Instance Method Details

#from_hash(hash_obj) ⇒ OpenStruct

Returns An OpenStruct object initialized by visiting hash_obj with recursion.

Examples:

obj = OpenStruct.from_hash(person: {name: 'John'})
obj.person.name  # => 'John'
obj.person.class # => OpenStruct

Parameters:

  • hash_obj (Hash)

    A Hash object

Returns:

  • (OpenStruct)

    An OpenStruct object initialized by visiting hash_obj with recursion



16
17
18
19
20
21
22
# File 'lib/llm/core_ext/ostruct.rb', line 16

def from_hash(hash_obj)
  visited_object = {}
  hash_obj.each do |key, value|
    visited_object[key] = walk(value)
  end
  OpenStruct.new(visited_object)
end