2014-12-04 154 views
0

我试图从本教程学习一些红宝石HTTP请求响应代码 -语法错误 - 意外tASSOC

http://danknox.github.io/2013/01/27/using-rubys-native-nethttp-library/

到目前为止的代码 -

require "net/http" 
require "uri" 

uri = URI.parse("http://api.random.com") 
http = Net::HTTP.new(uri.host, uri.port) 

# Continuing our example from above 

request = Net::HTTP::Get.new("/search?question=somequestion") 
response = http.request(request) 

response.code 
=>"200" 
response.body 
=> # Raw response body would go here needing to be parsed 

错误 -

Test.rb:13: syntax error, unexpected tASSOC, expecting $end 
=> "200" 
^

我不知道为什么会发生这种情况。我删除了空间,错误依然存在。我看到3-4堆栈溢出帖子,但他们没有帮助。

+0

什么是'=>“200”?它看起来像是从irb上剪切粘贴的东西。编辑:啊,从链接文章来看,似乎是这样,尽管作者没有说清楚。 – 2014-12-04 21:10:09

回答

2

您应该注释掉代码中以=>开头的两行。这些意味着是解释该方法的返回值应该是什么的注释,但不知何故,它们没有注释,Ruby解释器试图将它们解析为代码。

puts response.code # => "200" 
puts repsonse.body # => Raw response body 
+0

非常感谢。我现在得到一个错误未初始化的常量HTTPSuccess(NameError)。不知道我是否应该为此提出额外的问题。 – stack1 2014-12-04 21:22:39

+0

是的,一个额外的问题将是适当的。 – 2014-12-04 21:25:33