Skip to content
instance method

MatchData#values_at

(*::MatchData::capture | ::range[::int?] backrefs) -> ::Array[::String?]
values_at(*indexes) → array

Returns match and captures at the given indexes, which may include any mixture of:

  • Integers.

  • Ranges.

  • Names (strings and symbols).

Examples:

Example
m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
# => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
m.values_at(0, 2, -2) # => ["HX1138", "X", "113"]
m.values_at(1..2, -1) # => ["H", "X", "8"]

m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2")
# => #<MatchData "1 + 2" a:"1" op:"+" b:"2">
m.values_at(0, 1..2, :a, :b, :op)
# => ["1 + 2", "1", "+", "1", "2", "+"]
See also #to_s #to_a also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0