Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个Ruby代码在测试失败时执行?_Ruby - Fatal编程技术网

为什么这个Ruby代码在测试失败时执行?

为什么这个Ruby代码在测试失败时执行?,ruby,Ruby,在继续之前,我试图确保变量值是非nil的-该值是根据Sinatra实例上的请求活动异步实例化的 attr_accessor :access_token until !@access_token.nil? @access_token = RestClient.get @callback_URI + '/access_token/' + @request_Id end puts @access_token #=> always get output even if @access_t

在继续之前,我试图确保变量值是非nil的-该值是根据Sinatra实例上的请求活动异步实例化的

attr_accessor :access_token

until !@access_token.nil?
  @access_token = RestClient.get @callback_URI + '/access_token/' + @request_Id 
end

puts @access_token #=> always get output even if @access_token is nil

我的理解是,该表达式的意思是:“继续将RestClient调用的值赋给@access_令牌,直到它返回非nil值,然后退出直到块”。我做错了什么?非常感谢

假设您使用的是rest客户端gem,
RestClient
不会在请求失败时返回
nil
。例如:

require 'rest_client'
puts RestClient.get "http://invalidurl.foo/somepath"
从我的提供程序生成错误页(不是
nil
):


这意味着您的
until
-块始终只执行一次,因为
RestClient#get
始终具有返回值或抛出错误。为了帮助您,我需要了解有关您想要完成的任务的更多详细信息。

我不知道您的服务器返回了什么,但我会尝试以下操作:

while true
   begin
      @access_token = RestClient.get @callback_URI + '/access_token/' + @request_Id
      break if @access_token.code == 200
   rescue
      $stderr.puts "failed to get access token"
   end
   sleep 1
end

轮询不是异步的概念…为什么
until
而不是
while
?@MichaelKohl我必须是一个悲观主义者:)与
while
的结果相同,虽然..@Tass同意,但最终将使用NotificationsHanks padde重构此结果-但请求本身并没有“失败”,它是通过HTTP 200响应来实现的(OK)但是没有响应正文。然后尝试
while@access\u token.empty?
而不是您的
,直到
声明
while@access\u token.empty?
仍然没有运气,但是感谢您的建议。谢谢Steve,但是没有运气-我已经在Sinatra为
access\u token
没有赋值时设置了
状态204
,并确认
204
是Sinatra发送的响应。但仍然只有一个请求…非常奇怪,你的逻辑也没有问题!现在正在对Steeve的代码进行一个小的修改:swap
@access\u token.code==200
for
@访问\u token.code.empty?
并在我的实现中工作。谢谢大家的建议!
http.rb:762:in `initialize': getaddrinfo: nodename nor servname provided, or not known (SocketError)
while true
   begin
      @access_token = RestClient.get @callback_URI + '/access_token/' + @request_Id
      break if @access_token.code == 200
   rescue
      $stderr.puts "failed to get access token"
   end
   sleep 1
end