From b59917447cbf79981f59daa8c26831dc8f443064 Mon Sep 17 00:00:00 2001 From: Brandon Weaver Date: Sat, 8 Nov 2025 19:16:50 -0800 Subject: [PATCH 1/2] Add pattern matching support to Gem::Platform --- lib/rubygems/platform.rb | 6 ++++++ test/rubygems/test_gem_platform.rb | 34 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/rubygems/platform.rb b/lib/rubygems/platform.rb index e30c266fab1c..411512a465ef 100644 --- a/lib/rubygems/platform.rb +++ b/lib/rubygems/platform.rb @@ -146,6 +146,12 @@ def to_s 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 a3ae919809d3..0f1a715ab81a 100644 --- a/test/rubygems/test_gem_platform.rb +++ b/test/rubygems/test_gem_platform.rb @@ -683,4 +683,38 @@ def assert_local_match(name) 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 From 18f64c6b29df4111ff2aaddb5e3097f94e9e8f64 Mon Sep 17 00:00:00 2001 From: Brandon Weaver Date: Mon, 10 Nov 2025 21:58:41 -0800 Subject: [PATCH 2/2] Add documentation for pattern matching methods --- lib/rubygems/platform.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/rubygems/platform.rb b/lib/rubygems/platform.rb index 411512a465ef..f0eae47c5428 100644 --- a/lib/rubygems/platform.rb +++ b/lib/rubygems/platform.rb @@ -146,8 +146,33 @@ def to_s to_a.compact.join(@cpu.nil? ? "" : "-") end + ## + # Deconstructs the platform into an array for pattern matching. + # Returns [cpu, os, version]. + # + # Gem::Platform.new("x86_64-linux").deconstruct #=> ["x86_64", "linux", nil] + # + # This enables array pattern matching: + # + # case Gem::Platform.new("arm64-darwin-21") + # in ["arm64", "darwin", version] + # # version => "21" + # end alias_method :deconstruct, :to_a + ## + # Deconstructs the platform into a hash for pattern matching. + # Returns a hash with keys +:cpu+, +:os+, and +:version+. + # + # Gem::Platform.new("x86_64-darwin-20").deconstruct_keys(nil) + # #=> { cpu: "x86_64", os: "darwin", version: "20" } + # + # This enables hash pattern matching: + # + # case Gem::Platform.new("x86_64-linux") + # in cpu: "x86_64", os: "linux" + # # Matches Linux on x86_64 + # end def deconstruct_keys(keys) { cpu: @cpu, os: @os, version: @version } end