From 6b606cc806141c73e4be4671f5597e8625b6fb13 Mon Sep 17 00:00:00 2001 From: zhephyn Date: Thu, 3 Jul 2025 19:51:57 +0300 Subject: [PATCH 1/3] relocate _start_with? method to array.rb file --- lib/foobara/util/array.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/foobara/util/array.rb b/lib/foobara/util/array.rb index 6fae26d..c0ba2e1 100644 --- a/lib/foobara/util/array.rb +++ b/lib/foobara/util/array.rb @@ -36,5 +36,15 @@ def all_blank_or_false?(array) ) end end + + def _start_with?(large_array, small_array) + return false unless large_array.size > small_array.size + + small_array.each.with_index do |item, index| + return false unless large_array[index] == item + end + + true + end end end From 61ace115f768f7038f2d126247dec3ea685bef1c Mon Sep 17 00:00:00 2001 From: zhephyn Date: Fri, 4 Jul 2025 00:00:12 +0300 Subject: [PATCH 2/3] rename start_with? method with no prefix and add corresponding tests --- lib/foobara/util/array.rb | 2 +- spec/array_spec.rb | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/foobara/util/array.rb b/lib/foobara/util/array.rb index c0ba2e1..4f86624 100644 --- a/lib/foobara/util/array.rb +++ b/lib/foobara/util/array.rb @@ -37,7 +37,7 @@ def all_blank_or_false?(array) end end - def _start_with?(large_array, small_array) + def start_with?(large_array, small_array) return false unless large_array.size > small_array.size small_array.each.with_index do |item, index| diff --git a/spec/array_spec.rb b/spec/array_spec.rb index 666408a..3b052ab 100644 --- a/spec/array_spec.rb +++ b/spec/array_spec.rb @@ -36,4 +36,43 @@ it { is_expected.to be(true) } end end + + describe ".start_with?" do + subject { described_class.start_with?(large_array, small_array) } + + context "when size of large array is smaller than size of small array" do + let(:large_array) { [1, 2, 3] } + let(:small_array) { [1, 2, 3, 4, 5, 6, 7] } + + it { is_expected.to be(false) } + end + + context "when large array starts with elements from small array" do + let(:large_array) { [1, 2, 3, 4, 5, 6, 7] } + let(:small_array) { [1, 2, 3, 4] } + + it { is_expected.to be(true) } + end + + context "when elements in small array don't match those in large array" do + let(:large_array) { [1, 2, 3, 4, 5, 6, 7] } + let(:small_array) { [5, 6, 7, 8] } + + it { is_expected.to be(false) } + end + + context "when first elements in small array match those in the large array but last elements don't match" do + let(:large_array) { [1, 2, 3, 4, 5, 6, 7] } + let(:small_array) { [1, 2, 8, 9] } + + it { is_expected.to be(false) } + end + + context "when small array and large array are of the same size" do + let(:large_array) { [1, 2, 3, 4] } + let(:small_array) { [1, 2, 3, 4] } + + it { is_expected.to be(false) } + end + end end From 9206b85e942a9bc4523c1ac69a78e0069f8b7172 Mon Sep 17 00:00:00 2001 From: zhephyn Date: Tue, 8 Jul 2025 01:39:00 +0300 Subject: [PATCH 3/3] Add test to return true if large and small arrays are of same size --- lib/foobara/util/array.rb | 2 +- spec/array_spec.rb | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/foobara/util/array.rb b/lib/foobara/util/array.rb index 4f86624..318e4d3 100644 --- a/lib/foobara/util/array.rb +++ b/lib/foobara/util/array.rb @@ -38,7 +38,7 @@ def all_blank_or_false?(array) end def start_with?(large_array, small_array) - return false unless large_array.size > small_array.size + return false unless large_array.size >= small_array.size small_array.each.with_index do |item, index| return false unless large_array[index] == item diff --git a/spec/array_spec.rb b/spec/array_spec.rb index 3b052ab..a8469a5 100644 --- a/spec/array_spec.rb +++ b/spec/array_spec.rb @@ -72,7 +72,14 @@ let(:large_array) { [1, 2, 3, 4] } let(:small_array) { [1, 2, 3, 4] } - it { is_expected.to be(false) } + it { is_expected.to be(true) } + end + + context "when both arrays are empty" do + let(:large_array) { [] } + let(:small_array) { [] } + + it { is_expected.to be(true) } end end end