From 90e66daf73c6e47a136da11ccfceb529d4909b05 Mon Sep 17 00:00:00 2001 From: Herwin Date: Tue, 25 Jun 2024 11:40:16 +0200 Subject: [PATCH 1/3] Add specs for `for` loop with attribute target The specs with safe navigation require a segfault with MRI until very recent versions (https://bugs.ruby-lang.org/issues/20468), so these had to be wrapped in eval blocks. --- language/for_spec.rb | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/language/for_spec.rb b/language/for_spec.rb index 1d0750623..31d9a1c1a 100644 --- a/language/for_spec.rb +++ b/language/for_spec.rb @@ -114,6 +114,53 @@ class OFor $var = old_global_var end + it "allows an attribute as an iterator name" do + class OFor + attr_accessor :target + end + + ofor = OFor.new + m = [1,2,3] + n = 0 + for ofor.target in m + n += 1 + end + ofor.target.should == 3 + n.should == 3 + end + + ruby_version_is "3.4" do + it "allows an attribute with safe navigation as an iterator name" do + class OFor + attr_accessor :target + end + + ofor = OFor.new + m = [1,2,3] + n = 0 + eval <<~RUBY + for ofor&.target in m + n += 1 + end + RUBY + ofor.target.should == 3 + n.should == 3 + end + + it "allows an attribute with safe navigation on a nil base as an iterator name" do + ofor = nil + m = [1,2,3] + n = 0 + eval <<~RUBY + for ofor&.target in m + n += 1 + end + RUBY + ofor.should be_nil + n.should == 3 + end + end + # 1.9 behaviour verified by nobu in # http://redmine.ruby-lang.org/issues/show/2053 it "yields only as many values as there are arguments" do From c4a6ba40dc006e9bebeb714612ee717373cf1d6f Mon Sep 17 00:00:00 2001 From: Herwin Date: Tue, 25 Jun 2024 13:53:39 +0200 Subject: [PATCH 2/3] Add reference to bug report for segfault in for loop --- language/for_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/language/for_spec.rb b/language/for_spec.rb index 31d9a1c1a..786166e37 100644 --- a/language/for_spec.rb +++ b/language/for_spec.rb @@ -129,6 +129,7 @@ class OFor n.should == 3 end + # Segfault in MRI 3.3 and lower: https://bugs.ruby-lang.org/issues/20468 ruby_version_is "3.4" do it "allows an attribute with safe navigation as an iterator name" do class OFor From 6659170d74c459b8e7f3c47e2f67c42e441969bb Mon Sep 17 00:00:00 2001 From: Herwin Date: Tue, 25 Jun 2024 13:58:13 +0200 Subject: [PATCH 3/3] Add specs for `for` loop with index target --- language/for_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/language/for_spec.rb b/language/for_spec.rb index 786166e37..c8b43e25d 100644 --- a/language/for_spec.rb +++ b/language/for_spec.rb @@ -162,6 +162,28 @@ class OFor end end + it "allows an array index writer as an iterator name" do + arr = [:a, :b, :c] + m = [1,2,3] + n = 0 + for arr[1] in m + n += 1 + end + arr.should == [:a, 3, :c] + n.should == 3 + end + + it "allows a hash index writer as an iterator name" do + hash = { a: 10, b: 20, c: 30 } + m = [1,2,3] + n = 0 + for hash[:b] in m + n += 1 + end + hash.should == { a: 10, b: 3, c: 30 } + n.should == 3 + end + # 1.9 behaviour verified by nobu in # http://redmine.ruby-lang.org/issues/show/2053 it "yields only as many values as there are arguments" do