Rails and OpenURI

I am trying to run the following fragment from a new rails project in the console:

uri = URI.parse("http://25.media.tumblr.com/avatar_279ec8ee3427_64.png") data = open(uri) 

These errors are:

 TypeError: can't convert URI::HTTP into String from (irb):24:in `open' from (irb):24 from /Users/kevin/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start' from /Users/kevin/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start' from /Users/kevin/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands.rb:23:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>' 

I am running Rails 3.0.4 and Ruby 1.9.2. Any ideas how to fix this? Thanks!

+8
ruby ruby-on-rails
source share
2 answers

open-uri wants a string.

 data = open("http://25.media.tumblr.com/avatar_279ec8ee3427_64.png") 
+9
source share

open() will accept both a string and a URI.

 io = open("http://...") io = open(URI.parse("http://...")) 

The error you specified will occur if open-uri not enabled.

 require 'open-uri' 
+30
source share

All Articles