Ee0505bbd355292778077fb662c88f13

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?

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 !

59df2636047401adca41c11706e5da8a

Fester

June 19, 2010, June 19, 2010 13:42, permalink

3 ratings. Login to rate!
1
2
3
def parse_url(url)
  url[/\/([\d]{10})/, 1]
end
1ad8d2ae1c644eb22e8d1bbf5f81300f

Yury

June 19, 2010, June 19, 2010 16:37, permalink

1 rating. Login to rate!

[\d] same as \d

1
2
3
def parse_url(url)
  url[/\/(\d{10})/, 1]
end
Ee0505bbd355292778077fb662c88f13

Fu86

June 19, 2010, June 19, 2010 18:58, permalink

No rating. Login to rate!

Thanks a lot. Didn't know this syntax feature.
http://ruby-doc.org/core/classes/String.html#M000771

Your refactoring





Format Copy from initial code

or Cancel