传递给块的参数
Parameters passed to a block
rest_client gem 的文档有以下示例:
RestClient.get('http://my-rest-service.com/resource'){ |response, request, result, &block|
case response.code
when 200
p "It worked !"
response
when 423
raise SomeCustomExceptionIfYouWant
else
response.return!(request, result, &block)
end
}
你怎么知道上面每个块变量有哪些可用的属性? response
、request
等分别有什么属性?当我 运行 rest_client
:
response = RestClient.get('http://www.google.com')
很多东西返回为 response
:
response.instance_variables # => [:@net_http_res, :@args, :@request, :@code]
response.net_http_res # => #<Net::HTTPOK 200 OK readbody=true>
response.args # => {:method=>:get, :url=>"http://www.google.com", :headers=>{}}
response.code # => 200
response
的哪些部分可用于该块?参数的顺序重要吗?
通常,一个可选地接受块的方法会说这样的话:
def f(...)
if block_given?
...
yield thing1, thing2...
...
return foo
else
...
return bar
end
end
因此,出块的东西和没有出块的情况下返回的东西之间不需要有任何对应关系。
了解块接收什么的方法是查看方法的文档,或者查阅源代码。
是的,顺序很重要。 gem 文档特别指出 RestClient#get
为您提供 response
、request
、result
和 block
;它还描述了这些东西是什么。
我不知道你有什么特别的 gem,但一般来说,检查 class:
p response.class
然后查找关于class的文档,或者直接查看它有哪些方法:
p response.methods
来自restclient/request.rb
中的process_result
方法:
if block_given?
block.call(response, self, res, & block)
else
这是调用块的地方。因为这里的 self
是 Request
,所以给块的参数是 response
、request
、result
和 block
本身命令。
仅当 block_given?
为真时才调用该块。因此,就像您在问题中展示的那样,不带块地调用 RestClient.get
会赋予它不同的行为。
What parts of response are available to the block?
整个 Response
对象作为第一个参数提供给块。
Does the order of the parameters matter?
是的,参数的顺序很重要。
response = RestClient.get('http://www.google.com')
当你触发它时,你可以为整个可用列表提供类似 response.methods 的内容。这相当大,所以请参考 documentation/github 页面了解常用方法。所有方法的响应变量都可以直接使用和传递。
是的,参数的顺序很重要。
rest_client gem 的文档有以下示例:
RestClient.get('http://my-rest-service.com/resource'){ |response, request, result, &block|
case response.code
when 200
p "It worked !"
response
when 423
raise SomeCustomExceptionIfYouWant
else
response.return!(request, result, &block)
end
}
你怎么知道上面每个块变量有哪些可用的属性? response
、request
等分别有什么属性?当我 运行 rest_client
:
response = RestClient.get('http://www.google.com')
很多东西返回为 response
:
response.instance_variables # => [:@net_http_res, :@args, :@request, :@code]
response.net_http_res # => #<Net::HTTPOK 200 OK readbody=true>
response.args # => {:method=>:get, :url=>"http://www.google.com", :headers=>{}}
response.code # => 200
response
的哪些部分可用于该块?参数的顺序重要吗?
通常,一个可选地接受块的方法会说这样的话:
def f(...)
if block_given?
...
yield thing1, thing2...
...
return foo
else
...
return bar
end
end
因此,出块的东西和没有出块的情况下返回的东西之间不需要有任何对应关系。
了解块接收什么的方法是查看方法的文档,或者查阅源代码。
是的,顺序很重要。 gem 文档特别指出 RestClient#get
为您提供 response
、request
、result
和 block
;它还描述了这些东西是什么。
我不知道你有什么特别的 gem,但一般来说,检查 class:
p response.class
然后查找关于class的文档,或者直接查看它有哪些方法:
p response.methods
来自restclient/request.rb
中的process_result
方法:
if block_given?
block.call(response, self, res, & block)
else
这是调用块的地方。因为这里的 self
是 Request
,所以给块的参数是 response
、request
、result
和 block
本身命令。
仅当 block_given?
为真时才调用该块。因此,就像您在问题中展示的那样,不带块地调用 RestClient.get
会赋予它不同的行为。
What parts of response are available to the block?
整个 Response
对象作为第一个参数提供给块。
Does the order of the parameters matter?
是的,参数的顺序很重要。
response = RestClient.get('http://www.google.com')
当你触发它时,你可以为整个可用列表提供类似 response.methods 的内容。这相当大,所以请参考 documentation/github 页面了解常用方法。所有方法的响应变量都可以直接使用和传递。
是的,参数的顺序很重要。