Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MatchData#named_captures の symbolize_names オプションの説明を追加 #2895

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions refm/api/src/_builtin/MatchData
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,18 @@ m2 = r.match("abcabc", 0) # => #<MatchData "abc">
m1 == m2 # => true
#@end

#@since 3.3.0
--- named_captures(symbolize_names: false) -> Hash
#@else
--- named_captures -> Hash
#@end

名前付きキャプチャをHashで返します。

Hashのキーは名前付きキャプチャの名前です。Hashの値はキーの名前に対応した名前付きグループのうち最後にマッチした文字列です。

@param symbolize_names 真を指定するとハッシュのキーを文字列ではなくシンボルにします。デフォルトは偽です。

#@samplecode 例
m = /(?<a>.)(?<b>.)/.match("01")
m.named_captures # => {"a" => "0", "b" => "1"}
Expand All @@ -398,6 +404,11 @@ m.named_captures # => {"a" => "1"}

m = /(?<a>x)|(?<a>y)/.match("x")
m.named_captures # => {"a" => "x"}
#@since 3.3.0

m = /(?<a>.)(?<a>.)/.match("01")
m.named_captures(symbolize_names: true) #=> {:a => "1"}
#@end
#@end

@see [[m:MatchData#captures]]