Class: LLM::Object

Inherits:
BasicObject
Includes:
Enumerable
Defined in:
lib/llm/object.rb,
lib/llm/object/kernel.rb,
lib/llm/object/builder.rb

Overview

The LLM::Object class encapsulates a Hash object. It is similar in spirit to OpenStruct, and it was introduced after OpenStruct became a bundled gem rather than a default gem in Ruby 3.5.

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ LLM::Object

Parameters:

  • h (Hash) (defaults to: {})


19
20
21
# File 'lib/llm/object.rb', line 19

def initialize(h = {})
  @h = h.transform_keys(&:to_sym) || h
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &b) ⇒ Object (private)



74
75
76
77
78
79
80
81
82
# File 'lib/llm/object.rb', line 74

def method_missing(m, *args, &b)
  if m.to_s.end_with?("=")
    @h[m[0..-2].to_sym] = args.first
  elsif @h.key?(m)
    @h[m]
  else
    nil
  end
end

Instance Method Details

#digObject?

Returns:



68
69
70
# File 'lib/llm/object.rb', line 68

def dig(...)
  to_h.dig(...)
end

#each {|k, v| ... } ⇒ void

This method returns an undefined value.

Yields a key value pair to a block.

Yield Parameters:



28
29
30
# File 'lib/llm/object.rb', line 28

def each(&)
  @h.each(&)
end

#[](k) ⇒ Object

Parameters:

  • k (Symbol, #to_sym)

Returns:



35
36
37
# File 'lib/llm/object.rb', line 35

def [](k)
  @h[k.to_sym]
end

#[]=(k, v) ⇒ void

This method returns an undefined value.

Parameters:

  • k (Symbol, #to_sym)
  • v (Object)


43
44
45
# File 'lib/llm/object.rb', line 43

def []=(k, v)
  @h[k.to_sym] = v
end

#to_jsonString

Returns:

  • (String)


49
50
51
# File 'lib/llm/object.rb', line 49

def to_json(...)
  to_h.to_json(...)
end

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/llm/object.rb', line 55

def empty?
  @h.empty?
end

#to_hHash Also known as: to_hash

Returns:

  • (Hash)


61
62
63
# File 'lib/llm/object.rb', line 61

def to_h
  @h
end