File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -371,6 +371,46 @@ class RubyVM::InstructionSequence < Object
371371 # event_symbol] pair.
372372 #
373373 def trace_points : () -> Array[untyped ]
374+
375+ # <!--
376+ # rdoc-file=iseq.c
377+ # - of(p1)
378+ # -->
379+ # Returns the instruction sequence containing the given proc or method.
380+ #
381+ # For example, using irb:
382+ #
383+ # # a proc
384+ # > p = proc { num = 1 + 2 }
385+ # > RubyVM::InstructionSequence.of(p)
386+ # > #=> <RubyVM::InstructionSequence:block in irb_binding@(irb)>
387+ #
388+ # # for a method
389+ # > def foo(bar); puts bar; end
390+ # > RubyVM::InstructionSequence.of(method(:foo))
391+ # > #=> <RubyVM::InstructionSequence:foo@(irb)>
392+ #
393+ # Using ::compile_file:
394+ #
395+ # # /tmp/iseq_of.rb
396+ # def hello
397+ # puts "hello, world"
398+ # end
399+ #
400+ # $a_global_proc = proc { str = 'a' + 'b' }
401+ #
402+ # # in irb
403+ # > require '/tmp/iseq_of.rb'
404+ #
405+ # # first the method hello
406+ # > RubyVM::InstructionSequence.of(method(:hello))
407+ # > #=> #<RubyVM::InstructionSequence:0x007fb73d7cb1d0>
408+ #
409+ # # then the global proc
410+ # > RubyVM::InstructionSequence.of($a_global_proc)
411+ # > #=> #<RubyVM::InstructionSequence:0x007fb73d7caf78>
412+ #
413+ def self.of : (Proc | Method | UnboundMethod body) -> RubyVM::InstructionSequence?
374414end
375415
376416# <!-- rdoc-file=ast.rb -->
Original file line number Diff line number Diff line change 1+ require_relative "test_helper"
2+
3+ class RubyVM ::InstructionSequenceSingletonTest < Test ::Unit ::TestCase
4+ include TestHelper
5+
6+ testing "singleton(::RubyVM::InstructionSequence)"
7+
8+ def test_of
9+ assert_send_type "(::Method body) -> ::RubyVM::InstructionSequence" ,
10+ RubyVM ::InstructionSequence , :of , method ( :test_of )
11+ assert_send_type "(::UnboundMethod body) -> ::RubyVM::InstructionSequence" ,
12+ RubyVM ::InstructionSequence , :of , self . class . instance_method ( :test_of )
13+ assert_send_type "(::Proc body) -> ::RubyVM::InstructionSequence" ,
14+ RubyVM ::InstructionSequence , :of , -> { }
15+ end
16+ end
You can’t perform that action at this time.
0 commit comments