Module: LLM::Object::Builder

Included in:
LLM::Object
Defined in:
lib/llm/shell/internal/llm.rb/lib/llm/object/builder.rb

Instance Method Summary collapse

Instance Method Details

#from_hash(obj) ⇒ LLM::Object

Returns An LLM::Object object initialized by visiting ‘obj` with recursion.

Examples:

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

Parameters:

Returns:

  • (LLM::Object)

    An LLM::Object object initialized by visiting ‘obj` with recursion



16
17
18
19
20
21
22
23
24
25
# File 'lib/llm/shell/internal/llm.rb/lib/llm/object/builder.rb', line 16

def from_hash(obj)
  case obj
  when self then from_hash(obj.to_h)
  when Array then obj.map { |v| from_hash(v) }
  else
    visited = {}
    obj.each { visited[_1] = visit(_2) }
    new(visited)
  end
end