From 18207c9c913ff019254ccd56fd6542b54430bb46 Mon Sep 17 00:00:00 2001 From: Aditya Date: Fri, 6 Jun 2025 00:07:17 +0200 Subject: [PATCH 1/2] fix: check if matches is nil --- lib/countries/country/finder_methods.rb | 10 +++++----- spec/country_spec.rb | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/countries/country/finder_methods.rb b/lib/countries/country/finder_methods.rb index ea03c4e8..d78e51aa 100644 --- a/lib/countries/country/finder_methods.rb +++ b/lib/countries/country/finder_methods.rb @@ -46,12 +46,12 @@ def method_missing(method_name, *arguments) # :reek:BooleanParameter def respond_to_missing?(method_name, include_private = false) matches = method_name.to_s.match(FIND_BY_REGEX) + + return super unless matches + method = matches[3] - if matches && method - instance_methods.include?(method.to_sym) - else - super - end + + instance_methods.include?(method.to_sym) if method end protected diff --git a/spec/country_spec.rb b/spec/country_spec.rb index 7c382ee5..ba1c21aa 100644 --- a/spec/country_spec.rb +++ b/spec/country_spec.rb @@ -11,6 +11,10 @@ let(:country) { ISO3166::Country.search('US') } + it 'handles respond_to_missing values' do + expect(described_class.respond_to?(:arr)).not_to be_nil + end + it 'allows to create a country object from a symbol representation of the alpha2 code' do country = described_class.new(:us) expect(country.data).not_to be_nil From 65e9a11541fa09cac1a4cd3933e07db68f0d5067 Mon Sep 17 00:00:00 2001 From: Aditya Date: Sat, 7 Jun 2025 18:29:53 +0200 Subject: [PATCH 2/2] added condition to return false if method is nil - This change guarantees the return of a boolean --- lib/countries/country/finder_methods.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/countries/country/finder_methods.rb b/lib/countries/country/finder_methods.rb index d78e51aa..ba82f232 100644 --- a/lib/countries/country/finder_methods.rb +++ b/lib/countries/country/finder_methods.rb @@ -46,12 +46,11 @@ def method_missing(method_name, *arguments) # :reek:BooleanParameter def respond_to_missing?(method_name, include_private = false) matches = method_name.to_s.match(FIND_BY_REGEX) - - return super unless matches + return super unless matches && matches[3] method = matches[3] - instance_methods.include?(method.to_sym) if method + instance_methods.include?(method.to_sym) end protected