Undefined Path Method for StringIO in Ruby

I use the following snippet in a Rails application:

require 'open-uri'
url = "http://..."
uri = URI.parse(self.url)
file = open(uri)
puts "path: #{file.path}"

Which works with some files from the Internet, and then crashes on others:

undefined `path 'method for # <StringIO: 0x00000102a47240>

How to fix this strange, intermittent problem?

+3
source share
3 answers

Do not use Open :: URI like this.

Just do:

file = open(url)

Then you can readfile because you have an object of type IO:

body = file.read

or

body = open(url).read

If you need a path, parse the URL with a URI and get the path this way.

+7
source

I'm definitely late for the party, but ...

, open(url) 10 , -, Tempfile. StringIO, , path, .

(10kb) StringMax...

http://yard.ruby-doc.org/stdlib-2.1.0/OpenURI/Buffer.html

if defined?(OpenURI) && OpenURI::Buffer.const_defined?(StringMax)
  OpenURI::Buffer.send('remove_const', StringMax)
  OpenURI::Buffer.send('const_set', StringMax, 0)
end

, !

p.s. , #send, #remove_const #cont_set. p.p.s. , -, tempfiles, , , , StringIO. .

+6

docs SrtingIO dosnt .

+2

All Articles