Skip to content

Commit

Permalink
Merge pull request #528 from ruby/pass-block-to-block
Browse files Browse the repository at this point in the history
Pass block to block if given
  • Loading branch information
soutaro authored Dec 18, 2020
2 parents 385c968 + 20d5207 commit 78d04a2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/rbs/test/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,18 @@ def #{with_name}(*args, &block)
if block_given?
receiver = self
block_receives_block = block.parameters.last&.yield_self {|type, _| type == :block }
wrapped_block = proc do |*block_args|
wrapped_block = proc do |*block_args, &block2|
return_from_block = false
begin
block_result = if receiver.equal?(self)
yield(*block_args)
if block_receives_block
block.call(*block_args, &block2)
else
yield(*block_args)
end
else
instance_exec(*block_args, &block)
end
Expand Down
37 changes: 37 additions & 0 deletions test/rbs/test/runtime_test_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,43 @@ def call(val, &block)
end
A.new.call("foo") {|value:| puts value }
RUBY
end

def test_method_callbacks
assert_test_success(other_env: { 'RBS_TEST_TARGET' => "Callbacks" }, rbs_content: <<RBS, ruby_content: <<'RUBY')
module Callbacks
def on: (Symbol) { (*untyped) -> void } -> void
def emit: (Symbol event, *untyped args) ?{ (*untyped) -> void } -> void
end
RBS
module Callbacks
def on(e, &block)
(@callbacks[e] ||= []) << block
end
def emit(e, *args, &block)
@callbacks[e].each do |callable|
callable.call(*args, &block)
end
end
end
class A
def initialize
@callbacks = {}
end
include Callbacks
def bar(&block)
emit(:bar, &block)
end
end
a = A.new
a.on(:bar) { |*args, &block| puts "bar: #{args}"; block.call(self) }
a.emit(:bar, 1, 2) { |a| "this is A: #{a}" }
RUBY
end
end

0 comments on commit 78d04a2

Please sign in to comment.