Other APIs
These types are used less often than those in Main APIs
.
Topics
typealias RbBlockCallback
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 Rubybreak
; - Create and throw
RbException
instead of Rubyraise
; - Throwing any other kind of error (including propagating
RbError
s) 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
enum RbBlockRetention
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
public enum RbBlockRetention
struct RbBreak
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
public struct RbBreak: Error
struct RbProc
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
public struct RbProc: RbObjectConvertible
struct RbSymbol
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
public struct RbSymbol: RbObjectConvertible, Hashable
extension RbSymbol: CustomStringConvertible
enum RbThread
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 Thread
Thread
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
public enum RbThread
enum RbType
enum RbType