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