Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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