Skip to content

Commit

Permalink
Undefine VERSION in gemspec to avoid namespace pollution between mult…
Browse files Browse the repository at this point in the history
…iple MemoWises

This commit updates `memo_wise.gemspec` to undefine the `MemoWise::VERSION`
constant after it is read, as first suggested in
simplecov-ruby/simplecov#557 (comment).

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)
```
  • Loading branch information
JacobEvelyn committed Dec 23, 2024
1 parent 5f8d31d commit b06f1c6
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions memo_wise.gemspec
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit b06f1c6

Please sign in to comment.