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 5b2e31e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion memo_wise.gemspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# frozen_string_literal: true

# 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: Other uses of this trick often switch `require_relative` to `load`, as
# in https://github.com/simplecov-ruby/simplecov/issues/557#issuecomment-825171399,
# but for our purposes `require_relative` seems to work fine. If any issues
# arise, feel free to use `load` instead.
require_relative "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 5b2e31e

Please sign in to comment.