patternrubyMinor
iterate over an array and delete elements conditionally
Viewed 0 times
conditionallydeleteelementsarrayiterateandover
Problem
The following logic:
Iterate over the URLs array and If the HTTP request with the given URL does not timeout, continue. If the URL times out, remove it from the array and continue with the next one until the array is exhausted.
Has been implemented in the following way:
Anyone for a cleaner solution? Basically, I want to iterate over an array and remove elements from it if there is a timeout, but if the URL works loop should end and app should continue processing.
Iterate over the URLs array and If the HTTP request with the given URL does not timeout, continue. If the URL times out, remove it from the array and continue with the next one until the array is exhausted.
Has been implemented in the following way:
@urls.delete_if do |url|
begin
doc = perform_request(some_params)
break
rescue TimeoutError
Rails.logger.warn("URL #{url} times out, will be removed from list")
true
end
endAnyone for a cleaner solution? Basically, I want to iterate over an array and remove elements from it if there is a timeout, but if the URL works loop should end and app should continue processing.
Solution
note : your
Maybe it can just be a bit more expressive ?
You could even make
if you want to keep @tokland's functionnal approach, just use :
however, it would not be fully "functionnal", because
break is useless, because if the block returns a falsey value, the url is kept.Maybe it can just be a bit more expressive ?
def timeouts?( url )
perform_request( url ) && false
rescue TimeoutError
true
end
@urls.delete_if {|url| timeouts?( url ) }You could even make
timeouts? a method on your @urls objects, for extra readability : @urls.delete_if( &:timeouts? )if you want to keep @tokland's functionnal approach, just use :
@kept_urls = @urls.reject{|url| timeouts?( url ) }however, it would not be fully "functionnal", because
timeouts may not return the same results with the same arguments, depending on the execution environment.Code Snippets
def timeouts?( url )
perform_request( url ) && false
rescue TimeoutError
true
end
@urls.delete_if {|url| timeouts?( url ) }@urls.delete_if( &:timeouts? )@kept_urls = @urls.reject{|url| timeouts?( url ) }Context
StackExchange Code Review Q#20151, answer score: 3
Revisions (0)
No revisions yet.