What is a good way to get the filename from a URL in Ruby?

ruby, uri, url

Solution

require 'uri'

url = 'http://www.example.com/foo/bar/filename.jpg?2384973948743'

uri = URI.parse(url)

puts File.basename(uri.path)

#=> filename.jpg

Problem

What is a good way to extract `filename.jpg` from: ``` url = 'http://www.example.com/foo/bar/filename.jpg?2384973948743' ``` I'm using Ruby 1.9.3.

Original source