Environment: Ruby 1.9.2, Rails 3.0.3, Ubuntu
环境:Ruby 1.9.2,Rails 3.0.3,Ubuntu
When I try to open a URL using:
当我尝试使用以下命令打开URL时:
open("http://www.cnn.com")
I get the following error:
我收到以下错误:
Errno::ENOENT: No such file or directory - http://www.cnn.com
from (irb):9:in `initialize'
from (irb):9:in `open'
from (irb):9
(It's a difficult topic to search). This is happening in both irb and in my app. It used to work under Ruby 1.8.7 and Rails 2.3.4 but it appears that something has changed.
(这是一个难以搜索的主题)。这发生在irb和我的应用程序中。它曾经在Ruby 1.8.7和Rails 2.3.4下工作,但似乎有些东西发生了变化。
3 个解决方案
#1
23
I can reproduce the error if I try
如果我尝试,我可以重现错误
open('http://www.google.com')
I'll get this error: `initialize': No such file or directory - http://www.google.com (Errno::ENOENT)
我会收到这个错误:`initialize':没有这样的文件或目录 - http://www.google.com(Errno :: ENOENT)
Instead, I required 'open-uri' in ruby 1.9.2 and it worked -
相反,我在ruby 1.9.2中要求'open-uri'并且它有效 -
require 'open-uri'
url = URI.parse('http://www.google.com')
open(url) do |http|
response = http.read
puts "response: #{response.inspect}"
end
#2
2
I tried something like this in Codecademy's exercise section. Turns out that the request wanted a closing backslash. Evidently open('http://google.com/')
went through fine where open('http://google.com')
did not.
我在Codecademy的练习部分尝试了类似的东西。事实证明,该请求需要一个反斜杠。明显开放('http://google.com/')在开放('http://google.com')没有的情况下很好。
#3
0
I can't reproduce the error, in 1.8.7 I get a File object and in 1.9.2 I get a StringIO object. My guess is that some other code is overriding that functionality. Maybe you can try using the Net::HTTP object instead:
我无法重现错误,在1.8.7中我得到一个File对象,在1.9.2中我得到一个StringIO对象。我的猜测是其他一些代码覆盖了这个功能。也许您可以尝试使用Net :: HTTP对象:
require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('http://www.cnn.com')
or
要么
require 'net/http'
require 'uri'
url = URI.parse('http://www.cnn.com')
res = Net::HTTP.start(url.host, url.port) {|http|
http.get('/')
}
puts res.body
#1
23
I can reproduce the error if I try
如果我尝试,我可以重现错误
open('http://www.google.com')
I'll get this error: `initialize': No such file or directory - http://www.google.com (Errno::ENOENT)
我会收到这个错误:`initialize':没有这样的文件或目录 - http://www.google.com(Errno :: ENOENT)
Instead, I required 'open-uri' in ruby 1.9.2 and it worked -
相反,我在ruby 1.9.2中要求'open-uri'并且它有效 -
require 'open-uri'
url = URI.parse('http://www.google.com')
open(url) do |http|
response = http.read
puts "response: #{response.inspect}"
end
#2
2
I tried something like this in Codecademy's exercise section. Turns out that the request wanted a closing backslash. Evidently open('http://google.com/')
went through fine where open('http://google.com')
did not.
我在Codecademy的练习部分尝试了类似的东西。事实证明,该请求需要一个反斜杠。明显开放('http://google.com/')在开放('http://google.com')没有的情况下很好。
#3
0
I can't reproduce the error, in 1.8.7 I get a File object and in 1.9.2 I get a StringIO object. My guess is that some other code is overriding that functionality. Maybe you can try using the Net::HTTP object instead:
我无法重现错误,在1.8.7中我得到一个File对象,在1.9.2中我得到一个StringIO对象。我的猜测是其他一些代码覆盖了这个功能。也许您可以尝试使用Net :: HTTP对象:
require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('http://www.cnn.com')
or
要么
require 'net/http'
require 'uri'
url = URI.parse('http://www.cnn.com')
res = Net::HTTP.start(url.host, url.port) {|http|
http.get('/')
}
puts res.body