In Ruby, I want to store some stuff in a Hash, but I don’t want it to be case-sensitive. So for example:
h = Hash.new
h["HELLO"] = 7
puts h["hello"]
This should output 7, even though the case is different. Can I just override the equality method of the hash or something similar?
Thanks.
To prevent this change from completely breaking independent parts of your program (such as other ruby gems you are using), make a separate class for your insensitive hash.
After overriding the assignment and retrieval functions, it’s pretty much cake. Creating a full replacement for Hash would require being more meticulous about handling the aliases and other functions (for example, #has_key? and #store) needed for a complete implementation. The pattern above can easily be extended to all these related methods.