ruby / ruby/rbs

Specify the type of evaluation context of a block

Open
#654 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.2k
Forks
256
Avg merge
6d 17h
Merged PRs (30d)
37

Description

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

https://github.com/ruby/rbs/blob/287808c00f7809e0c1261074c6eb90990d8d1d21/core/basic_object.rbs#L172-L185

  # 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked core/basic_object.rbs signature for instance_exec, then inspect the RBS specs, examples, and source code mentioned in the issue for existing block-context syntax. Determine whether a block evaluation context can be represented and document or implement the supported form, including the Sequel::Model dataset_module example as the completion criterion.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.