[ruby-core:78263] [Ruby trunk Feature#11999] MatchData#to_h to get a Hash from named captures

From: headius@...
Date: 2016-11-22 19:41:55 UTC
List: ruby-core #78263
Issue #11999 has been updated by Charles Nutter.


Shouldn't this produce Symbol keys?

----------------------------------------
Feature #11999: MatchData#to_h to get a Hash from named captures
https://bugs.ruby-lang.org/issues/11999#change-61627

* Author: sorah Shota Fukumori
* Status: Closed
* Priority: Normal
* Assignee: sorah Shota Fukumori
----------------------------------------
~~~
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-2.diff (3.41 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next