Skip to content

Specify the type of evaluation context of a block #654

@TSMMark

Description

@TSMMark

In this example, the binding / evaluation context of the block is said to be self

rbs/core/basic_object.rbs

Lines 172 to 185 in 287808c

# Executes the given block within the context of the receiver (*obj*). In order
# to set the context, the variable `self` is set to *obj* while the code is
# executing, giving the code access to *obj*'s instance variables. Arguments
# are passed as block parameters.
#
# class KlassWithSecret
# def initialize
# @secret = 99
# end
# end
# k = KlassWithSecret.new
# k.instance_exec(5) {|x| @secret+x } #=> 104
#
def instance_exec: [U, V] (*V args) { (*V args) -> U } -> U

  # Executes the given block within the context of the receiver (*obj*). In order
  # to set the context, the variable `self` is set to *obj* while the code is
  # executing, giving the code access to *obj*'s instance variables.  Arguments
  # are passed as block parameters.
  #
  #     class KlassWithSecret
  #       def initialize
  #         @secret = 99
  #       end
  #     end
  #     k = KlassWithSecret.new
  #     k.instance_exec(5) {|x| @secret+x }   #=> 104
  #
  def instance_exec: [U, V] (*V args) { (*V args) -> U } -> U

However, I believe self is the default context of a block — But if I want to specify a context other than self for a block, how can that be accomplished?

I've searched through many specs, examples, and source code in this repo and can't find out how to do it.


Example:

Using Sequel gem, Sequel::Model has a class method: dataset_module, which receives a block. Methods defined in this block will have the evaluation context of an instance of a Sequel::Dataset.

I've tried many different variations of a syntax like this with no success:

def self.dataset_module: () { (self: Sequel::Dataset ) -> void } -> void

Is this currently unsupported by RBS or am I just missing it in the documentation?

Any advice, direction, or link to source code exemplifying this would be very much appreciated — thank you in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions