Class: LLM::Object
- Inherits:
- BasicObject
- Extended by:
- Builder
- Includes:
- Enumerable, Kernel
- 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, and it allows a consumer to get and set Hash keys via regular methods. It is similar in spirit to OpenStruct, and it was introduced after OpenStruct became a bundled gem (and not a default gem) in Ruby 3.5.
Defined Under Namespace
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ void
- #dig ⇒ Object?
-
#each {|k, v| ... } ⇒ void
Yields a key|value pair to a block.
- #empty? ⇒ Boolean
- #initialize(h = {}) ⇒ LLM::Object constructor
- #to_h ⇒ Hash (also: #to_hash)
- #to_json ⇒ String
Methods included from Builder
Methods included from Kernel
#class, #extend, #inspect, #instance_of?, #kind_of?, #method, #object_id, #pretty_print, #respond_to?, #respond_to_missing?, #tap
Constructor Details
#initialize(h = {}) ⇒ LLM::Object
20 21 22 |
# File 'lib/llm/object.rb', line 20 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)
75 76 77 78 79 80 81 82 83 |
# File 'lib/llm/object.rb', line 75 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
#[]=(k, v) ⇒ void
This method returns an undefined value.
44 45 46 |
# File 'lib/llm/object.rb', line 44 def []=(k, v) @h[k.to_sym] = v end |
#each {|k, v| ... } ⇒ void
This method returns an undefined value.
Yields a key|value pair to a block.
29 30 31 |
# File 'lib/llm/object.rb', line 29 def each(&) @h.each(&) end |
#empty? ⇒ Boolean
56 57 58 |
# File 'lib/llm/object.rb', line 56 def empty? @h.empty? end |
#to_h ⇒ Hash Also known as: to_hash
62 63 64 |
# File 'lib/llm/object.rb', line 62 def to_h @h end |
#to_json ⇒ String
50 51 52 |
# File 'lib/llm/object.rb', line 50 def to_json(...) to_h.to_json(...) end |