From 4adb012926f8bd6011168327d8832cf19976de40 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Thu, 18 Nov 2021 12:44:19 -0800 Subject: Anonymous block forwarding allows a method to forward a passed block to another method without having to provide a name for the block parameter. Implements [Feature #11256] Co-authored-by: Yusuke Endoh mame@ruby-lang.org Co-authored-by: Nobuyoshi Nakada nobu@ruby-lang.org --- test/ruby/test_iseq.rb | 18 ++++++++++++++++++ test/ruby/test_parse.rb | 2 +- test/ruby/test_syntax.rb | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) (limited to 'test/ruby') diff --git a/test/ruby/test_iseq.rb b/test/ruby/test_iseq.rb index 692549efa0..49f12019dc 100644 --- a/test/ruby/test_iseq.rb +++ b/test/ruby/test_iseq.rb @@ -125,6 +125,16 @@ class TestISeq < Test::Unit::TestCase assert_equal(42, ISeq.load_from_binary(iseq.to_binary).eval) end + def test_super_with_anonymous_block + iseq = compile(<<~EOF) + def touch3(&block) # :nodoc: + foo { super } + end + 42 + EOF + assert_equal(42, ISeq.load_from_binary(iseq.to_binary).eval) + end + def test_ractor_unshareable_outer_variable name = "\u{2603 26a1}" y = eval("proc {#{name} = nil; proc {|x| #{name} = x}}").call @@ -373,6 +383,14 @@ class TestISeq < Test::Unit::TestCase assert_equal [2], param_names end + def anon_block(&); end + + def test_anon_block_param_in_disasm + iseq = RubyVM::InstructionSequence.of(method(:anon_block)) + param_names = iseq.to_a[iseq.to_a.index(:method) + 1] + assert_equal [:&], param_names + end + def strip_lineno(source) source.gsub(/^.*?: /, "") end diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index 3120016e60..d697a29c1c 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -1044,7 +1044,7 @@ x = __ENCODING__ end; assert_syntax_error("def\nf(000)end", /^ \^~~/) - assert_syntax_error("def\nf(&)end", /^ \^/) + assert_syntax_error("def\nf(&0)end", /^ \^/) end def test_method_location_in_rescue diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index 11953ab563..ce1e489992 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -66,6 +66,18 @@ class TestSyntax < Test::Unit::TestCase f&.close! end + def test_anonymous_block_forwarding + assert_syntax_error("def b; c(&); end", /no anonymous block parameter/) + assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}") + begin; + def b(&); c(&) end + def c(&); yield 1 end + a = nil + b{|c| a = c} + assert_equal(1, a) + end; + end + def test_newline_in_block_parameters bug = '[ruby-dev:45292]' ["", "a", "a, b"].product(["", ";x", [";", "x"]]) do |params| -- cgit v1.2.3