From 5d3b691f675b8ba9efad1ab36436a84eefbea0f6 Mon Sep 17 00:00:00 2001 From: Matteo Date: Fri, 29 May 2026 22:51:31 +0200 Subject: [PATCH] fix(encoder): replace values_at with string slicing to avoid latent nil values_at(0, 2..consonants.size) silently returns nil for the out-of-bounds index and join discards it, producing the correct result by accident. Replace with the explicit string-slice idiom, which is Ruby 2.5-safe and expresses the intent clearly. Co-Authored-By: Claude Sonnet 4.6 --- lib/itax_code/encoder.rb | 2 +- test/itax_code/encoder_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/itax_code/encoder.rb b/lib/itax_code/encoder.rb index 18d1031..6204607 100644 --- a/lib/itax_code/encoder.rb +++ b/lib/itax_code/encoder.rb @@ -63,7 +63,7 @@ def encode_name consonants = utils.extract_consonants chars vowels = utils.extract_vowels chars - consonants = consonants.chars.values_at(0, 2..consonants.size).join if consonants.length > 3 + consonants = (consonants[0] + consonants[2..-1]).to_s if consonants.length > 3 "#{consonants[0..2]}#{vowels[0..2]}XXX"[0..2].upcase end diff --git a/test/itax_code/encoder_test.rb b/test/itax_code/encoder_test.rb index 2e30ff5..e536931 100644 --- a/test/itax_code/encoder_test.rb +++ b/test/itax_code/encoder_test.rb @@ -43,6 +43,17 @@ class EncoderTest < Minitest::Test ).encode end + test "#encode when name has 5 consonants skips the 2nd consonant" do + assert_equal "RSSVNT80A01F205L", + Encoder.new( + surname: "Rossi", + name: "Valentino", + gender: "M", + birthdate: "1980-01-01", + birthplace: "Milano" + ).encode + end + test "#encode for foreign countries" do assert_equal "BRRDRN70M41Z602D", Encoder.new(