DEV Community


Posted on

Rails HashWithIndifferentAccess magical

Alt Text

In daily development the parameters passed to the action in the Controller when implementing business logic will be placed in the params object. generally we can access with

params[:token] #=> token_value
params["token"] #=> token_value
Enter fullscreen mode Exit fullscreen mode

You will find that the value of key in params can be access by using Symbol or String.

Is it a bit magical?

In fact the principle is very simple params have the characteristics extend from HashWithIndifferentAccess

class HashWithIndifferentAccess < Hash
    def []=(key, value)
        regular_writer(convert_key(key), convert_value(value, for: :assignment))

    # key point 
    def convert_key(key) 
       key.kind_of?(Symbol) ? key.to_s : key

    def self.[](*args)
Enter fullscreen mode Exit fullscreen mode

Through the above source code analysis, it can be seen that for a HashWithIndifferentAccess object, when assigning it key is uniformly converted into String type (covert_key method).

When a value is to be taken, the key of the Symbol will be forced to be converted to String so that the user can use Symbol ** or **String to take the same value.

Hope it can hlep you :)

Top comments (0)