Created
June 8, 2013 08:44
-
-
Save berlysia/5734556 to your computer and use it in GitHub Desktop.
MatchData#to_h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MatchData | |
def to_h | |
ret = Hash.new | |
self.regexp.named_captures.each{|key,valindex| | |
ret[key] = valindex.collect{|i| | |
self.to_a[i] | |
} | |
ret[key].delete(nil) | |
case ret[key].size | |
when 1 then ret[key] = ret[key][0] | |
when 0 then ret[key] = nil | |
end | |
} | |
return ret | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MatchDataからHashが欲しかったのでつくった。named_captureしたやつのことしか考えてない。