1 2 3 4 5
def parse_url(url) matches = url.match(/\/([\d]{10})/) return nil unless matches matches[1] end
Refactorings
No refactoring yet !
Fester
June 19, 2010, June 19, 2010 13:42, permalink
1 2 3
def parse_url(url) url[/\/([\d]{10})/, 1] end
Yury
June 19, 2010, June 19, 2010 16:37, permalink
[\d] same as \d
1 2 3
def parse_url(url) url[/\/(\d{10})/, 1] end
Fu86
June 19, 2010, June 19, 2010 18:58, permalink
Thanks a lot. Didn't know this syntax feature.
http://ruby-doc.org/core/classes/String.html#M000771
I want to return nil if nothing matched and the second item in the resulting array if matched. How can i write this more elegant?