What is the difference between Ruby's Hash and ActiveSupport's HashWithIndifferentAccess?

What is the difference between Ruby's Hash and ActiveSupport's HashWithIndifferentAccess?



The Hash class in Ruby's core library retrieves values by doing a standard == comparison on the keys. This means that a value stored for a Symbol key (e.g. :my_value) cannot be retrieved using the equivalent String (e.g. 'my_value'). On the other hand, HashWithIndifferentAccess treats Symbol keys and String keys as equivalent so that the following would work:

h = HashWithIndifferentAccess.new
h[:my_value] = 'foo'
h['my_value'] #=> will return "foo"

Comments

Popular posts from this blog

Examine the collapse of Han. What factors explain how this occurred? How influential in Han Dynasty on Chinese History?

Summarize effective communication strategies in organizational hierarchies, and review the role and relevance of the organizational grapevine

Describe at least three of the general information skills essential for all business careers.