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 || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



125
126
127
128
129
130
131
132
133
# File 'lib/llm/object.rb', line 125

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

Instance Method Details

#[](k) ⇒ Object

Parameters:

  • k (Symbol, #to_sym)

Returns:



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

def [](k)
  @h[key(k)]
end

#[]=(k, v)

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_s] = v
end

#digObject?

Returns:



113
114
115
# File 'lib/llm/object.rb', line 113

def dig(...)
  @h.dig(...)
end

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

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

#each_pair {|| ... } ⇒ Object

Yield Parameters:



107
108
109
# File 'lib/llm/object.rb', line 107

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @h.empty?
end

#fetch(k, *args, &b) ⇒ Object

Parameters:

  • k (String, Symbol)

Returns:



94
95
96
# File 'lib/llm/object.rb', line 94

def fetch(k, *args, &b)
  @h.fetch(key(k), *args, &b)
end

#key?(k) ⇒ Boolean Also known as: has_key?

Parameters:

  • k (String, Symbol)

Returns:

  • (Boolean)


86
87
88
# File 'lib/llm/object.rb', line 86

def key?(k)
  @h.key?(key(k))
end

#keysArray<String>

Returns:

  • (Array<String>)


73
74
75
# File 'lib/llm/object.rb', line 73

def keys
  @h.keys
end

#sizeInteger Also known as: length

Returns:

  • (Integer)


100
101
102
# File 'lib/llm/object.rb', line 100

def size
  @h.size
end

#sliceHash

Returns:

  • (Hash)


119
120
121
# File 'lib/llm/object.rb', line 119

def slice(...)
  @h.slice(...)
end

#to_hHash

Returns:

  • (Hash)


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

def to_h
  @h.dup
end

#to_hashHash

Returns:

  • (Hash)


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

def to_hash
  @h.transform_keys(&:to_sym)
end

#to_jsonString

Returns:

  • (String)


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

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

#valuesArray

Returns:

  • (Array)


79
80
81
# File 'lib/llm/object.rb', line 79

def values
  @h.values
end