diff options
| -rw-r--r-- | lib/rubygems/platform.rb | 6 | ||||
| -rw-r--r-- | test/rubygems/test_gem_platform.rb | 34 |
2 files changed, 40 insertions, 0 deletions
diff --git a/lib/rubygems/platform.rb b/lib/rubygems/platform.rb index e30c266fab..411512a465 100644 --- a/lib/rubygems/platform.rb +++ b/lib/rubygems/platform.rb @@ -146,6 +146,12 @@ class Gem::Platform to_a.compact.join(@cpu.nil? ? "" : "-") end + alias_method :deconstruct, :to_a + + def deconstruct_keys(keys) + { cpu: @cpu, os: @os, version: @version } + end + ## # Is +other+ equal to this platform? Two platforms are equal if they have # the same CPU, OS and version. diff --git a/test/rubygems/test_gem_platform.rb b/test/rubygems/test_gem_platform.rb index a3ae919809..0f1a715ab8 100644 --- a/test/rubygems/test_gem_platform.rb +++ b/test/rubygems/test_gem_platform.rb @@ -683,4 +683,38 @@ class TestGemPlatform < Gem::TestCase def refute_local_match(name) refute_match Gem::Platform.local, name end + + def test_deconstruct + platform = Gem::Platform.new("x86_64-linux") + assert_equal ["x86_64", "linux", nil], platform.deconstruct + end + + def test_deconstruct_keys + platform = Gem::Platform.new("x86_64-darwin-20") + assert_equal({ cpu: "x86_64", os: "darwin", version: "20" }, platform.deconstruct_keys(nil)) + end + + def test_pattern_matching_array + platform = Gem::Platform.new("arm64-darwin-21") + result = + case platform + in ["arm64", "darwin", version] + version + else + "no match" + end + assert_equal "21", result + end + + def test_pattern_matching_hash + platform = Gem::Platform.new("x86_64-linux") + result = + case platform + in cpu: "x86_64", os: "linux" + "matched" + else + "no match" + end + assert_equal "matched", result + end end |
