Skip to content

Instantly share code, notes, and snippets.

@malclocke
Created February 10, 2015 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malclocke/caeedde0d7d739f6811c to your computer and use it in GitHub Desktop.
Save malclocke/caeedde0d7d739f6811c to your computer and use it in GitHub Desktop.
require 'delegate'
class CaseInsensitiveHashWriter < SimpleDelegator
def []=(key, value)
return __getobj__[key] unless key.respond_to?(:downcase)
matched_key = keys.detect do |k|
k.respond_to?(:downcase) && k.downcase == key.downcase
end
if matched_key
__getobj__[matched_key] = value
else
__getobj__[key] = value
end
end
end
h = {'fOO' => 'bar' }
cihc = CaseInsensitiveHashWriter.new(h)
cihc['Foo'] = 'baz'
h = cihc.to_h
puts h.class
puts h.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment