Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ 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

## [1.1.17] - 2026-10-25
Expand Down
6 changes: 4 additions & 2 deletions lib/version_gem/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,7 +65,7 @@ def to_a
#
# @return [Array<String>]
def _to_a
@_to_a = self::VERSION.split(".")
@_to_a ||= self::VERSION.split(".")
end
end
end
29 changes: 29 additions & 0 deletions spec/version_gem/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,33 @@
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

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