From c10dcd55f32b50c9a8199e49f431ff241021192c Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 2 Apr 2026 12:45:22 +0200 Subject: [PATCH] Add signature for `RubyVM::InstructionSequence.of` https://docs.ruby-lang.org/en/4.0/RubyVM/InstructionSequence.html#method-c-of `prism` uses it and currently vendors this change. The return type is nillable as per the implementation in ruby but I'm not sure how to actually trigger that for valid input. It may happen with some procs, that's all I know. --- core/ruby_vm.rbs | 40 +++++++++++++++++++ .../stdlib/RubyVM_InstructionSequence_test.rb | 16 ++++++++ 2 files changed, 56 insertions(+) create mode 100644 test/stdlib/RubyVM_InstructionSequence_test.rb diff --git a/core/ruby_vm.rbs b/core/ruby_vm.rbs index 09cc2058c..2d60024d5 100644 --- a/core/ruby_vm.rbs +++ b/core/ruby_vm.rbs @@ -371,6 +371,46 @@ class RubyVM::InstructionSequence < Object # event_symbol] pair. # def trace_points: () -> Array[untyped] + + # + # Returns the instruction sequence containing the given proc or method. + # + # For example, using irb: + # + # # a proc + # > p = proc { num = 1 + 2 } + # > RubyVM::InstructionSequence.of(p) + # > #=> + # + # # for a method + # > def foo(bar); puts bar; end + # > RubyVM::InstructionSequence.of(method(:foo)) + # > #=> + # + # Using ::compile_file: + # + # # /tmp/iseq_of.rb + # def hello + # puts "hello, world" + # end + # + # $a_global_proc = proc { str = 'a' + 'b' } + # + # # in irb + # > require '/tmp/iseq_of.rb' + # + # # first the method hello + # > RubyVM::InstructionSequence.of(method(:hello)) + # > #=> # + # + # # then the global proc + # > RubyVM::InstructionSequence.of($a_global_proc) + # > #=> # + # + def self.of: (Proc | Method | UnboundMethod body) -> RubyVM::InstructionSequence? end # diff --git a/test/stdlib/RubyVM_InstructionSequence_test.rb b/test/stdlib/RubyVM_InstructionSequence_test.rb new file mode 100644 index 000000000..7436fd55c --- /dev/null +++ b/test/stdlib/RubyVM_InstructionSequence_test.rb @@ -0,0 +1,16 @@ +require_relative "test_helper" + +class RubyVM::InstructionSequenceSingletonTest < Test::Unit::TestCase + include TestHelper + + testing "singleton(::RubyVM::InstructionSequence)" + + def test_of + assert_send_type "(::Method body) -> ::RubyVM::InstructionSequence", + RubyVM::InstructionSequence, :of, method(:test_of) + assert_send_type "(::UnboundMethod body) -> ::RubyVM::InstructionSequence", + RubyVM::InstructionSequence, :of, self.class.instance_method(:test_of) + assert_send_type "(::Proc body) -> ::RubyVM::InstructionSequence", + RubyVM::InstructionSequence, :of, -> { } + end +end