Closed agrobbin closed 3 years ago
Gem::Version#canonical_segments memoizes the array, which is different than segments, which returns a dup.
Gem::Version#canonical_segments
segments
dup
Since we are manipulating the array to do this calculation, we should not modify canonical_segments itself.
canonical_segments
I ran into this since Gem::Version objects are essentially value objects, cached across the system based on the string:
Gem::Version
Gem::Version.new('6.0.3.4') === Gem::Version.new('6.0.3.4') # => true
Because of that, when doing my own inspection of Rails.gem_version.canonical_segments after activerecord-pg_enum had been installed, the canonical_segments array had been modified.
Rails.gem_version.canonical_segments
install
Consider this scenario:
Rails.gem_version # => #<Gem::Version "6.0.3.4"> Rails.gem_version.canonical_segments # => [6, 0, 3, 4] Rails.gem_version > Gem::Version.new('6.0') # => true # activerecord-pg_enum is installed Rails.gem_version.canonical_segments # => [6, 0] Rails.gem_version > Gem::Version.new('6.0') # => false
@alassek I've also issued a PR to RubyGems itself (rubygems/rubygems#4247) to prevent this from happening in the future.
Woah! Good catch. I definitely did not intend this.
Gem::Version#canonical_segments
memoizes the array, which is different thansegments
, which returns adup
.Since we are manipulating the array to do this calculation, we should not modify
canonical_segments
itself.I ran into this since
Gem::Version
objects are essentially value objects, cached across the system based on the string:Because of that, when doing my own inspection of
Rails.gem_version.canonical_segments
after activerecord-pg_enum had beeninstall
ed, thecanonical_segments
array had been modified.Consider this scenario: