tags:

views:

214

answers:

4

Let's say that I want to download this picture using ruby. How do I do that?

http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg

I am using mac.

+6  A: 

require "open-uri"

open("your-url") {|f|
   File.open("whatever_file.jpg","wb") do |file|
     file.puts f.read
   end
}


Geo
You need to add "require 'open-uri'" for this to work.
Keltia
yeah, I forgot to add it in the first version.
Geo
tangentially, if you're on Windows (unlike the OP), you'll need to open the file in binary mode: File.open(filename, 'w').binmode do |file| ...
glenn jackman
Good observation. I'll update the answer.
Geo
A: 

The same way you download anything else. Net::HTTP

David Dorward
+1  A: 
%x(wget http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg)

or

`wget http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg`
Maximiliano Guzman
this is not portable.
Geo
maybe. But he asked something that would work on Mac.
Maximiliano Guzman
A: 

The easiest way would be to require open-uri and use that with the previous answer or use the also supplied Net::HTTP module with its get method.

Keltia