From b06f1c6964e3d1ef715a256e4f1595e98ea2fbc0 Mon Sep 17 00:00:00 2001 From: Jacob Evelyn Date: Mon, 23 Dec 2024 12:18:03 -0500 Subject: [PATCH] Undefine VERSION in gemspec to avoid namespace pollution between multiple MemoWises This commit updates `memo_wise.gemspec` to undefine the `MemoWise::VERSION` constant after it is read, as first suggested in https://github.com/simplecov-ruby/simplecov/issues/557#issuecomment-825171399. Before this change, accessing `MemoWise::VERSION` immediately after the `doff_and_don` call in `benchmarks.rb` gives the version of the gem pulled from GitHub, even though `doff_and_don` should mean nothing is in the `MemoWise` namespace. After this change, accessing `MemoWise::VERSION` there gives this error as we expect: ``` uninitialized constant MemoWise (NameError) ``` --- memo_wise.gemspec | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/memo_wise.gemspec b/memo_wise.gemspec index 2817681..d706800 100644 --- a/memo_wise.gemspec +++ b/memo_wise.gemspec @@ -1,10 +1,25 @@ # frozen_string_literal: true -require_relative "lib/memo_wise/version" +# After loading the `VERSION` constant, save it to a variable to use in this +# gemspec and then undefine it. This allows benchmarks to compare multiple +# versions of MemoWise against each other without inadvertently sharing the same +# `VERSION` constant. +# NOTE: It's important that we use `load` instead of `require_relative` because +# the latter only loads a file once, and so by undefining the `VERSION` constant +# we can make it difficult to access that value again later. For more context, +# see: https://github.com/panorama-ed/memo_wise/pull/370#issuecomment-2560268423 +# NOTE: If we ever bump the minimum Ruby version to 3.1+, we can simplify this +# code and use this instead: +# +# spec.version = Module.new.tap do |mod| +# load("lib/memo_wise/version.rb", mod) +# end::MemoWise::VERSION +load "lib/memo_wise/version" +gem_version = MemoWise.send(:remove_const, :VERSION) Gem::Specification.new do |spec| spec.name = "memo_wise" - spec.version = MemoWise::VERSION + spec.version = gem_version spec.summary = "The wise choice for Ruby memoization" spec.homepage = "https://github.com/panorama-ed/memo_wise" spec.license = "MIT"