Swift Method APIs

These types are used to implement Ruby methods in Swift.


Topics

struct RbMethodArgsSpec

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.

Declaration
Swift
public struct RbMethodArgsSpec: Sendable

typealias RbMethodCallback

The function signature for a Ruby method implemented as a Swift free function or closure.

The RbObject is the object the method is being invoked against. The RbMethod provides useful services such as argument access.

You can throw an RbException to raise a Ruby exception instead of returning normally from the method. Throwing another type gets wrapped up in an RbException and raised as a Ruby runtime exception.

See RbBoundMethodCallback and RbBoundMethodVoidCallback for use with custom Ruby classes that are bound to Swift types.

Declaration
Swift
public typealias RbMethodCallback = (RbObject, RbMethod) throws -> RbObject

typealias RbBoundMethodCallback

The function signature for a Ruby method implemented as a Swift method of a Swift bound object that returns a value.

These classes are defined with RbGateway.defineClass(_:under:initializer:) and methods on them defined with RbObject.defineMethod(_:argsSpec:method:).

This typealias describe methods on the type SwiftPeer that take a single RbMethod and return some type that can convert to RbObject. The SwiftPeer is the instance associated with the Ruby object; the RbMethod provides useful services such as argument access.

You can throw an RbException to raise a Ruby exception instead of returning normally from the method. Throwing another type gets wrapped up in an RbException and raised as a Ruby runtime exception.

Declaration
Swift
public typealias RbBoundMethodCallback<
    SwiftPeer: AnyObject & Sendable, Return: RbObjectConvertible & Sendable
> =
    @Sendable (SwiftPeer) -> (RbMethod) throws -> Return

typealias RbBoundMethodVoidCallback

The function signature for a Ruby method implemented as a Swift method of a Swift bound object that does not return a value.

These classes are defined with RbGateway.defineClass(_:under:initializer:) and methods on them defined with RbObject.defineMethod(_:argsSpec:method:).

This typealias describe methods on the type SwiftPeer that take a single RbMethod. The SwiftPeer is the instance associated with the Ruby object; the RbMethod provides useful services such as argument access.

You can throw an RbException to raise a Ruby exception instead of returning normally from the method. Throwing another type gets wrapped up in an RbException and raised as a Ruby runtime exception.

Declaration
Swift
public typealias RbBoundMethodVoidCallback<SwiftPeer: AnyObject & Sendable> =
    @Sendable (SwiftPeer) -> (RbMethod) throws -> Void

struct RbMethod

This offers useful services to Swift implementations of Ruby methods.

You do not create instances of this type: instead, RubyGateway creates instances and passes them to method callbacks.

Declaration
Swift
public struct RbMethod: Sendable

struct RbMethodArgs

The various types of argument passed to a Ruby method implemented in Swift.

Available via RbMethod.args when the method is invoked.

Declaration
Swift
public struct RbMethodArgs: Sendable