summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrandon Weaver <keystonelemur@gmail.com>2025-11-08 19:16:50 -0800
committergit <svn-admin@ruby-lang.org>2025-11-12 22:23:10 +0000
commit3efabc8355df489d1fa9717e7cb1b8daa8ddf92e (patch)
treeeb648fe8973be16b002e80922d31d481c2273510
parentcdc3faa6b3e02d9f40d9e1ff356bee9ad01ceb73 (diff)
[ruby/rubygems] Add pattern matching support to Gem::Platform
https://github.com/ruby/rubygems/commit/b59917447c
-rw-r--r--lib/rubygems/platform.rb6
-rw-r--r--test/rubygems/test_gem_platform.rb34
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