在哈希,我可以使用我可以爲Ruby的MatchData設置默認值嗎?
map = Hash.new("(0,0)")
或
map = Hash.new()
map.default = "(0, 0)"
設置爲未定義鍵的默認值,這樣,當我嘗試檢索未定義鍵的值,我不會得到一個錯誤。但在MatchData中,例如:
line = "matchBegins\/blabla\" = (20, 10);"
get = line.match(/matchBegins\/(?<match1>\D*)" *= *(?<match2>.*);/)
puts get[:notExisted]
我會收到一個錯誤。我檢查過MatchData的文檔,但不能設置任何設置默認值。我對麼?由於
你是指'(?P \ D *)'? –