Other APIs

These types are used less often than those in Main APIs.


Topics

typealias RbBlockCallback

The type of a block implemented in Swift.

The parameter is an array of arguments passed to the block. If the block has just one argument then this is still an array.

The interface doesn’t support other types of argument, for example keyword or optional.

Blocks always return a value. You can use RbObject.nilObject if you have nothing useful to return.

Blocks can throw errors:

  • Create and throw RbBreak instead of Ruby break;
  • Create and throw RbException instead of Ruby raise;
  • Throwing any other kind of error (including propagating RbErrors) causes RubyGateway to convert the error into a Ruby RuntimeError exception and raise it.

See RbObjectAccess.call(_:args:kwArgs:blockRetention:blockCall:) and RbObject.init(blockCall:).

Declaration
Swift
public typealias RbBlockCallback = ([RbObject]) throws -> RbObject

enum RbBlockRetention

Control over how Swift closures passed as blocks are retained.

When you pass a Swift closure as a block, for example using RbObjectAccess.call(_:args:kwArgs:blockRetention:blockCall:), RubyGateway needs some help to understand how Ruby will use the closure.

The easiest thing to get wrong is using .none when Ruby retains the block for use later. This causes a hard crash in RbBlockContext.from(raw:) when Ruby tries to call the block.

Declaration
Swift
public enum RbBlockRetention

struct RbBreak

Throwing an instance of this type terminates and gives an overall result to a Ruby block-based iteration like the Ruby break keyword.

let result = myobj.call("each") { args in
                 let derived = f(args[0])
                 if g(derived) {
                     throw RbBreak(with: derived)
                 }
                 return .nilObject
             }
Declaration
Swift
public struct RbBreak: Error

struct RbProc

A Ruby Proc.

Use this to create a Ruby Proc from a symbol or any Ruby object supporting to_proc.

This is most useful when passing a block to a method:

// Ruby: mapped = names.map(&:downcase)
let mapped = names.call("map", block: RbProc(RbSymbol("downcase")))

Use RbObject.init(blockCall:) to create a Ruby Proc from a Swift closure.

If you want to pass Swift code to a method as a block then just call RbObjectAccess.call(_:args:kwArgs:blockRetention:blockCall:) directly, no need for either RbProc or RbObject.

Declaration
Swift
public struct RbProc: RbObjectConvertible

struct RbSymbol

Represent a Ruby symbol.

Ruby symbols are written :name. If in Ruby you would write:

obj.meth(:value)

Then the RubyGateway version is:

try obj.call("meth", args: [RbSymbol("value")])
Declaration
Swift
public struct RbSymbol: RbObjectConvertible, Hashable

extension RbSymbol: CustomStringConvertible

enum RbThread

This type provides a namespace for working with Ruby threads.

You cannot call Ruby on arbitrary threads: only the very first thread where RubyGateway gets used or threads created by Ruby’s ThreadThread class.

There is no way to ‘attach’ the Ruby runtime to a thread created by client code (eg. one accessed via libdispatch).

Even when multiple Ruby threads are active, the VM executes just one at a time under control of a single lock known as the GVL. The GVL is given up over Ruby blocking operations and can be manually relinquished using RbThread.callWithoutGvl(callback:).

Inside a block that has given up the GVL, you must not call any Ruby code or it will at best crash. You can execute some code inside your GVL-free scope that is allowed to call Ruby using RbThread.callWithGvl(callback:).

Declaration
Swift
public enum RbThread

enum RbType

The type of a Ruby VALUE as wrapped by RbObject.

Not generally useful, maybe for debugging.

Declaration
Swift
public enum RbType: Int32