I am calling the RestClient::Resource#get(additional_headers = {}, &block) method multiple times with the same block but on different Resources, I was wondering if there is a way to save the block into a variable, or to save it into a Proc convert it into a block each time.
Edit:
I did the following:
resource = RestClient::Resource.new('https://foo.com')
redirect = lambda do |response, request, result, &block|
if [301, 302, 307].include? response.code
response.follow_redirection(request, result, &block)
else
response.return!(request, result, &block)
end
end
@resp = resource.get (&redirect)
I get: Syntax error, unexpected tAMPER
Placing an ampersand in front of an item in a method call, e.g.
a.map!(&:to_i)calls theto_procmethod on that object and passes the resulting proc as a block. Some alternative forms of defining your re-usable block:If you are calling a method with a block and you want to save that block for re-use later, you can do so by using an ampersand in the method definition to capture the block as a proc parameter: