Skip to content

Instantly share code, notes, and snippets.

@nbfritz
Created December 14, 2012 15:37
Show Gist options
  • Select an option

  • Save nbfritz/4286314 to your computer and use it in GitHub Desktop.

Select an option

Save nbfritz/4286314 to your computer and use it in GitHub Desktop.
Ruby Hash Post - Part 2: ranges CAN work as keys
STATUS_TYPES = Hash.new {|this_hash,missing_key|
found_key = this_hash.keys.find {|this_key| this_key.class == Range && this_key.include?(missing_key) }
found_key ? this_hash[missing_key] = this_hash[found_key] : :undefined
}.merge({
200..208 => :success,
500..511 => :server_error
})
STATUS_TYPES.keys # => [200..208, 500..511]
STATUS_TYPES[200] # => :success
STATUS_TYPES[999] # => :undefined
STATUS_TYPES.keys # => [200..208, 500..511, 200]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment