Best way to extract last segment of URI in Ruby

Try these:

if url =~ /\/(.+?)$/
  last = $1
end

Or

last = File.basename(url)

While all the usages of split suggested in the answers here are legit, in my opinion @matsko's answer is the one with the clearer code to read:

last = File.basename(url)

I would use a proper URI parser like the one of the URI module to get the path from the URI. Then split it at / and get the last part of it:

require 'uri'

URI(uri).path.split('/').last

uri.split('/')[-1] 

or

uri.split('/').last