mirror of
https://github.com/ruby/ruby.git
synced 2026-01-28 13:04:22 +00:00
[ruby/rubygems] Add documentation for pattern matching methods
https://github.com/ruby/rubygems/commit/18f64c6b29
This commit is contained in:
parent
3efabc8355
commit
2247b0becb
@ -146,8 +146,33 @@ class Gem::Platform
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user