Project

General

Profile

Actions

Feature #11999

closed

MatchData#to_h to get a Hash from named captures

Added by sorah (Sorah Fukumori) over 6 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:72897]

Description

class MatchData
  def to_h
    self.names.map { |n| [n, self[n]] }.to_h
  end
end

p '12'.match(/(?<a>.)(?<b>.)(?<c>.)?/).to_h #=> {"a"=>"1", "b"=>"2", "c"=>nil}

Sometimes I want to get a Hash from named capture, but currently I have to use #names + #captures. How about adding MatchData#to_h for convenience way?


Files

11999.diff (2.77 KB) 11999.diff sorah (Sorah Fukumori), 01/18/2016 08:55 AM
11999-2.diff (3.41 KB) 11999-2.diff Based on specification at [ruby-core:73839] sorah (Sorah Fukumori), 02/16/2016 12:35 PM
Actions

Also available in: Atom PDF