Skip to content

Instantly share code, notes, and snippets.

@berlysia
Created June 8, 2013 08:44
Show Gist options
  • Save berlysia/5734556 to your computer and use it in GitHub Desktop.
Save berlysia/5734556 to your computer and use it in GitHub Desktop.
MatchData#to_h
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
@berlysia
Copy link
Author

berlysia commented Jun 8, 2013

MatchDataからHashが欲しかったのでつくった。named_captureしたやつのことしか考えてない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment