Module: LLM::Utils
Overview
Shared utility methods used across the runtime.
Instance Method Summary collapse
-
#resolve_option(obj, option, resolve_symbol: true) ⇒ Object
Resolves a configured option against an object instance.
-
#normalize_base_path(path) ⇒ String
Normalizes an HTTP API base path.
-
#name_of(obj) ⇒ String?
Returns the Ruby module or class name for an object.
-
#object_id(obj) ⇒ String
Renders the class-and-object-id portion of an inspect string.
Instance Method Details
#resolve_option(obj, option, resolve_symbol: true) ⇒ Object
Resolves a configured option against an object instance.
Proc values are evaluated with instance_exec, symbol values are
optionally sent to the object as method calls, hashes are duplicated,
and all other values are returned as-is.
20 21 22 23 24 25 26 27 |
# File 'lib/llm/utils.rb', line 20 def resolve_option(obj, option, resolve_symbol: true) case option when Proc then obj.instance_exec(&option) when Symbol then resolve_symbol ? obj.send(option) : option when Hash then option.dup else option end end |
#normalize_base_path(path) ⇒ String
Normalizes an HTTP API base path.
Blank paths normalize to an empty string. Non-empty paths are prefixed with a leading slash and stripped of trailing slashes.
37 38 39 40 41 42 |
# File 'lib/llm/utils.rb', line 37 def normalize_base_path(path) path = path.to_s.strip return "" if path.empty? || path == "/" path = "/#{path}" unless path.start_with?("/") path.sub(%r{/+\z}, "") end |
#name_of(obj) ⇒ String?
Returns the Ruby module or class name for an object.
This bypasses overridden #name implementations by binding
Module#name directly.
52 53 54 |
# File 'lib/llm/utils.rb', line 52 def name_of(obj) ::Module.instance_method(:name).bind(obj).call end |
#object_id(obj) ⇒ String
Renders the class-and-object-id portion of an inspect string.
This returns strings like LLM::Tool:0x1234abcd, which can be
embedded into custom inspect output.
64 65 66 67 68 69 70 71 |
# File 'lib/llm/utils.rb', line 64 def object_id(obj) klass = if Class === obj name_of(obj) || name_of(obj.superclass) || obj.class.name else obj.class.name || obj.class.to_s end "#{klass}:0x#{obj.object_id.to_s(16)}" end |