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
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
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
35
36
37
|
# File 'lib/llm/object.rb', line 35
def [](k)
@h[key(k)]
end
|
#[]=(k, v)
This method returns an undefined value.
43
44
45
|
# File 'lib/llm/object.rb', line 43
def []=(k, v)
@h[k.to_s] = v
end
|
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.
28
29
30
|
# File 'lib/llm/object.rb', line 28
def each(&)
@h.each(&)
end
|
#each_pair {|| ... } ⇒ Object
107
108
109
|
# File 'lib/llm/object.rb', line 107
def each_pair(&)
@h.each(&)
end
|
#empty? ⇒ Boolean
55
56
57
|
# File 'lib/llm/object.rb', line 55
def empty?
@h.empty?
end
|
#fetch(k, *args, &b) ⇒ Object
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?
86
87
88
|
# File 'lib/llm/object.rb', line 86
def key?(k)
@h.key?(key(k))
end
|
#keys ⇒ Array<String>
73
74
75
|
# File 'lib/llm/object.rb', line 73
def keys
@h.keys
end
|
#size ⇒ Integer
Also known as:
length
100
101
102
|
# File 'lib/llm/object.rb', line 100
def size
@h.size
end
|
#slice ⇒ Hash
119
120
121
|
# File 'lib/llm/object.rb', line 119
def slice(...)
@h.slice(...)
end
|
#to_h ⇒ Hash
61
62
63
|
# File 'lib/llm/object.rb', line 61
def to_h
@h.dup
end
|
#to_hash ⇒ Hash
67
68
69
|
# File 'lib/llm/object.rb', line 67
def to_hash
@h.transform_keys(&:to_sym)
end
|
#to_json ⇒ String
49
50
51
|
# File 'lib/llm/object.rb', line 49
def to_json(...)
to_h.to_json(...)
end
|
#values ⇒ Array
79
80
81
|
# File 'lib/llm/object.rb', line 79
def values
@h.values
end
|