While working on something recently, I started to think about the efficiency of Arrays and Ranges in Ruby. I started to try and research this but could find very little information on it or even how I could test this myself.
So I came across some code that checks what range a HTTP status code is in, and it's written something like this
SUCCESS = (200...300)
REDIRECTION = (300...400)
if SUCCESS.include?(status_code)
status = 'success'
elsif REDIRECTION.include?(status_code)
status = 'redirection'
end
So this got me thinking that it seems wasteful to use 200...300 when we essentially only need 200...207, but would there a big efficiency difference in this, if any at all?
Also what about the 4XX codes, as it is not always a straight run of the range, it got me thinking that maybe I should turn it into an array, so I could write it one of two ways
As a straight range
CLIENT_ERROR = (400...429)
or as an array
CLIENT_ERROR = [*(400...419), 422, 429]
I'm assuming the first option is a better approach and more efficient but just not too sure how to validate my thoughts, so any advice or input on this would be greatly appreciated