1

I want to write a method (in Ruby/Rails) that takes a URL (String) to a text file and uses JSON to parse it.

# LOAD entity file def load (url) ` result = JSON.parse(x) end

What should x be? (I know it needs to be either a String or an IO object, but how do I get a string/IO object whose content is the content of the file that url points to?

4

1 に答える 1

1
require 'open-uri'

def load(url)
  open(url) do |file|
    JSON.parse(file.read)
  end
end
于 2013-02-13T20:55:52.653 に答える