Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

MatchData#names

() -> ::Array[::String]
names → array_of_names

Returns an array of the capture names (see Named Captures):

Example
m = /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge")
# => #<MatchData "hog" foo:"h" bar:"o" baz:"g">
m.names # => ["foo", "bar", "baz"]

m = /foo/.match('foo') # => #<MatchData "foo">
m.names # => [] # No named captures.

Equivalent to:

Example
m = /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge")
m.regexp.names # => ["foo", "bar", "baz"]
See also #named_captures #offset #match_length #post_match also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0