Skip to content
Tags

,

Has v/s HasWithIndifferentAccess

August 3, 2011

Here’s why if params[:blog_entry] returns a value, then params[“blog_entry”] returns the same value.
http://rubyquicktips.com/post/603292403/accessing-a-hash-with-either-string-or-symbol-keys

params belongs to a different class
> params.class
=> ActiveSupport::HashWithIndifferentAccess
> Hash.class
=> Hash

More fun:

> hash = {"name" => "Gagan"}
=> {"name"=>"Gagan"}
> hash.class
=> Hash
> hash["name"]
=> "Gagan"
> hash[:name]
=> nil
> hash = hash.with_indifferent_access
=> {"name"=>"Gagan"}
> hash[:name]
=> "Gagan"
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: