---
title: "Ruby 2.4 MatchData#values_at"
description:
  "Ruby 2.4 adds ability to extract named capture groups through values_at"
canonical_url: "https://www.bigbinary.com/blog/ruby-2.4-adds-matchdata-values-at-for-extracting-named-and-positional-capture-groups"
markdown_url: "https://www.bigbinary.com/blog/ruby-2.4-adds-matchdata-values-at-for-extracting-named-and-positional-capture-groups.md"
---

# Ruby 2.4 MatchData#values_at

Ruby 2.4 adds ability to extract named capture groups through values_at

- Author: Rohit Kumar
- Published: December 21, 2016
- Categories: Ruby 2.4, Ruby

### Ruby 2.3

We can use `MatchData#[]` to extract named capture and positional capture
groups.

```ruby

pattern=/(?<number>\d+) (?<word>\w+)/
pattern.match('100 thousand')[:number]
#=> "100"

pattern=/(\d+) (\w+)/
pattern.match('100 thousand')[2]
#=> "thousand"

```

Positional capture groups could also be extracted using `MatchData#values_at`.

```ruby

pattern=/(\d+) (\w+)/
pattern.match('100 thousand').values_at(2)
#=> ["thousand"]

```

### Changes in Ruby 2.4

In Ruby 2.4, we can pass string or symbol to extract named capture groups to
method `#values_at`.

```ruby

pattern=/(?<number>\d+) (?<word>\w+)/
pattern.match('100 thousand').values_at(:number)
#=> ["100"]

```

## Links

- [Human page](https://www.bigbinary.com/blog/ruby-2.4-adds-matchdata-values-at-for-extracting-named-and-positional-capture-groups)
