RbMethodArgsSpec

Swift
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

The number of leading mandatory positional arguments.

Declaration
Swift
public let leadingMandatoryCount: Int

var optionalValues: [@Sendable () -> RbObject]

Default values for all optional positional arguments.

Declaration
Swift
public let optionalValues: [@Sendable () -> RbObject]

var optionalCount: Int

The number of optional positional arguments.

Declaration
Swift
public var optionalCount: Int { get }

var supportsSplat: Bool

Does the method support variable-length splatted arguments?

Declaration
Swift
public let supportsSplat: Bool

var trailingMandatoryCount: Int

The number of trailing mandatory positional arguments.

Declaration
Swift
public let trailingMandatoryCount: Int

var totalMandatoryCount: Int

The number of all mandatory positional arguments.

Declaration
Swift
public var totalMandatoryCount: Int { get }

var mandatoryKeywords: Set<String>

Names of mandatory keyword arguments.

Declaration
Swift
public let mandatoryKeywords: Set<String>

var optionalKeywordValues: [String : @Sendable () -> RbObject]

Names and default values of optional keyword arguments.

Declaration
Swift
public let optionalKeywordValues: [String: @Sendable () -> RbObject]

var supportsKeywords: Bool

Does the method support keyword arguments?

Declaration
Swift
public var supportsKeywords: Bool { get }

var requiresBlock: Bool

Does the method require a block?

Declaration
Swift
public let 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
Swift
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, false by default.

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, false by default. If this is true then the method may or may not be called with a block.

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
Swift
public static func basic(_ count: Int) -> RbMethodArgsSpec
Parameters
count

the number of arguments not including any block that the method must be passed.