RbMethodArgsSpec
public struct RbMethodArgsSpec: Sendable
A description of how a Ruby method implemented in Swift is supposed to be called.
Ruby supports several different ways of passing arguments. A single method can support a mixture of positional, keyword, and variable-length (splatted) arguments.
You supply one of these when defining a method so that RubyGateway knows how to decode
the arguments passed to it before your RbMethodCallback
is invoked. The decoded
arguments are stored in the args
property of the RbMethod
passed to your callback.
If you want to say “accept any number of arguments” then write
RbMethodArgsSpec(supportsSplat: true)
and access the arguments via method.args.splatted
.
Topics
var leadingMandatoryCount: Int
var leadingMandatoryCount: Int
The number of leading mandatory positional arguments.
Declaration
public let leadingMandatoryCount: Int
var optionalValues: [@Sendable () -> RbObject]
var optionalValues: [@Sendable () -> RbObject]
var optionalCount: Int
var optionalCount: Int
The number of optional positional arguments.
Declaration
public var optionalCount: Int { get }
var supportsSplat: Bool
var supportsSplat: Bool
Does the method support variable-length splatted arguments?
Declaration
public let supportsSplat: Bool
var trailingMandatoryCount: Int
var trailingMandatoryCount: Int
The number of trailing mandatory positional arguments.
Declaration
public let trailingMandatoryCount: Int
var totalMandatoryCount: Int
var totalMandatoryCount: Int
The number of all mandatory positional arguments.
Declaration
public var totalMandatoryCount: Int { get }
var mandatoryKeywords: Set<String>
var mandatoryKeywords: Set<String>
var optionalKeywordValues: [String : @Sendable () -> RbObject]
var optionalKeywordValues: [String : @Sendable () -> RbObject]
var supportsKeywords: Bool
var supportsKeywords: Bool
Does the method support keyword arguments?
Declaration
public var supportsKeywords: Bool { get }
var requiresBlock: Bool
var requiresBlock: Bool
init(leadingMandatoryCount: Int, optionalValues: [(any RbObjectConvertible & Sendable)?], supportsSplat: Bool, trailingMandatoryCount: Int, mandatoryKeywords: Set<String>, optionalKeywordValues: [String : (any RbObjectConvertible & Sendable)?], requiresBlock: Bool)
init(leadingMandatoryCount: Int, optionalValues: [(any RbObjectConvertible & Sendable)?], supportsSplat: Bool, trailingMandatoryCount: Int, mandatoryKeywords: Set<String>, optionalKeywordValues: [String : (any RbObjectConvertible & Sendable)?], requiresBlock: Bool)
Create a new method arguments specification.
The default values here are evaluated lazily: each time the method is invoked and requires the
default argument because caller has not provided it, the RbObjectConvertible.rubyObject
is evaluated and provided to the method. In the case of constant defaults this is unobservable but
does give correct behaviour if you actually supply something that turns into a Ruby expression.
Declaration
public init(
leadingMandatoryCount: Int = 0,
optionalValues: [(any RbObjectConvertible & Sendable)?] = [],
supportsSplat: Bool = false,
trailingMandatoryCount: Int = 0,
mandatoryKeywords: Set<String> = [],
optionalKeywordValues: [String: (any RbObjectConvertible & Sendable)?] =
[:],
requiresBlock: Bool = false)
Parameters
leadingMandatoryCount |
The number of leading mandatory positional arguments, none by default. |
optionalValues |
The default values for optional positional arguments, none by default. |
supportsSplat |
Whether the method supports splatted variable-length args,
|
trailingMandatoryCount |
The number of trailing mandatory positional arguments, none by default. |
mandatoryKeywords |
The names of mandatory keyword arguments, none by default. |
optionalKeywordValues |
The default values for optional keyword arguments, none by default. |
requiresBlock |
Whether the method requires a block, |
static func basic(Int) -> RbMethodArgsSpec
static func basic(Int) -> RbMethodArgsSpec
Helper to quickly create a spec for a method with a fixed number of arguments.
Example usage:
try myClass.defineMethod(name: "addScore", argsSpec: .basic(1)) { ...
}
Declaration
public static func basic(_ count: Int) -> RbMethodArgsSpec
Parameters
count |
the number of arguments not including any block that the method must be passed. |