RbBlockRetention

Swift
public 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 the default of .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.


Topics

case none

Do not retain the closure. The default, appropriate when the block is used only during execution of the method it is passed to. For example #each.

Declaration
Swift
case none

case `self`

Retain the closure for as long as the object that owns the method. Use when a method stores a closure in an object property for later use.

Declaration
Swift
case `self`

case returned

Retain the closure for as long as the object returned by the method. Use when the method is a factory that produces some object and passes that object the closure. For example Proc#new.

Declaration
Swift
case returned