Skip to content

Commit f1803ab

Browse files
authored
Merge pull request #2916 from Earlopain/rubyvm-is-of
Add signature for `RubyVM::InstructionSequence.of`
2 parents 43cfd5e + 4f21193 commit f1803ab

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

core/ruby_vm.rbs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff 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?
374414
end
375415

376416
# <!-- rdoc-file=ast.rb -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

0 commit comments

Comments
 (0)