From 0d18a2ed1ceae9fa108f4ae0089446cafe780e79 Mon Sep 17 00:00:00 2001 From: joe-sharp Date: Mon, 2 Nov 2026 01:18:36 -0600 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Memoize=20split=20vers?= =?UTF-8?q?ion=20string=20in=20=5Fto=5Fa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _to_a assigned with = instead of ||=, so the VERSION string was split again on every call to major, minor, patch, and pre. Memoize it like the other cached helpers. Co-Authored-By: Claude Haiku 5.5 Signed-off-by: joe-sharp --- CHANGELOG.md | 2 ++ lib/version_gem/api.rb | 2 +- spec/version_gem/api_spec.rb | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c0df86..d76dae8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ Please file a bug if you notice a violation of semantic versioning. ### Fixed +- `_to_a` now memoizes the split version string, so `VERSION` is split once instead of on every component lookup + ### Security ## [1.1.17] - 2026-10-25 diff --git a/lib/version_gem/api.rb b/lib/version_gem/api.rb index 50ec55c..1bfd3f4 100755 --- a/lib/version_gem/api.rb +++ b/lib/version_gem/api.rb @@ -63,7 +63,7 @@ def to_a # # @return [Array] def _to_a - @_to_a = self::VERSION.split(".") + @_to_a ||= self::VERSION.split(".") end end end diff --git a/spec/version_gem/api_spec.rb b/spec/version_gem/api_spec.rb index 38f9d54..5f34e9c 100644 --- a/spec/version_gem/api_spec.rb +++ b/spec/version_gem/api_spec.rb @@ -34,4 +34,23 @@ expect(under_test.to_a).to(eq([12, 34, 56, "pre-78"])) end end + + context "when memoizing lookups" do + let(:version_string) { "1.2.3.pre-78".dup } + let(:version_module) do + Module.new.tap do |mod| + mod.const_set(:VERSION, version_string) + mod.extend(described_class) + end + end + + it "splits the version string only once across component lookups" do + expect(version_string).to(receive(:split).once.and_call_original) + + version_module.major + version_module.minor + version_module.patch + version_module.pre + end + end end From 4cc77d4fc92e0f2b86c2ce09e21692463a52d42a Mon Sep 17 00:00:00 2001 From: joe-sharp Date: Mon, 2 Nov 2026 01:18:52 -0600 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Cache=20nil=20pre-rele?= =?UTF-8?q?ase=20in=20pre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ||= does not memoize nil, so pre re-ran _to_a on every call for versions without a pre-release suffix. Check defined?(@pre) instead. Co-Authored-By: Claude Haiku 5.5 Signed-off-by: joe-sharp --- CHANGELOG.md | 1 + lib/version_gem/api.rb | 4 +++- spec/version_gem/api_spec.rb | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d76dae8..37e022e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ Please file a bug if you notice a violation of semantic versioning. ### Fixed - `_to_a` now memoizes the split version string, so `VERSION` is split once instead of on every component lookup +- `pre` now caches a `nil` pre-release, so versions without a suffix no longer re-run the lookup on every call ### Security diff --git a/lib/version_gem/api.rb b/lib/version_gem/api.rb index 1bfd3f4..7c2fe89 100755 --- a/lib/version_gem/api.rb +++ b/lib/version_gem/api.rb @@ -35,7 +35,9 @@ def patch # # @return [String, NilClass] def pre - @pre ||= _to_a[3] + return @pre if defined?(@pre) + + @pre = _to_a[3] end # The version number as a hash diff --git a/spec/version_gem/api_spec.rb b/spec/version_gem/api_spec.rb index 5f34e9c..4949af1 100644 --- a/spec/version_gem/api_spec.rb +++ b/spec/version_gem/api_spec.rb @@ -52,5 +52,15 @@ version_module.patch version_module.pre end + + context "without a pre-release" do + let(:version_string) { "1.2.3".dup } + + it "looks up the pre-release only once" do + expect(version_module).to(receive(:_to_a).once.and_call_original) + + 3.times { version_module.pre } + end + end end end