# Param values are HashWithIndifferentAccess?

**URL:** <https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919>\
**Category:** Rails\
**Created:** [September 7, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919 "2006-09-07T21:41:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 7, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919/1 "2006-09-07T21:41:56Z")

</div>

I have the following code:

params.each do |k, v|  
if k =~ /something/  
ARClass.find\_by\_blah(v)  
end  
end

The “find\_by\_blah” call fails to return anything and when I print out  
v.class.name, I get HashWithIndifferentAccess.

WTF? Is it unreasonable for me to expect the value of a params hash  
element to be String?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 7, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919/2 "2006-09-07T22:11:14Z")

</div>

Wes G. wrote:

> params.each do |k, v|  
> if k =~ /something/  
> ARClass.find\_by\_blah(v)  
> end  
> end
> 
> The “find\_by\_blah” call fails to return anything and when I print out  
> v.class.name, I get HashWithIndifferentAccess.
> 
> WTF? Is it unreasonable for me to expect the value of a params hash  
> element to be String?

Try…

params.each do |k, v|  
if k.to\_s =~ /something/  
…  
end  
end

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 7, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919/3 "2006-09-07T22:30:03Z")

</div>

Ruby has an intersting problem with accessing Hashes with Symbols vs  
Strings. Right now Symbols and Strings are in fact two different  
objects.  
Rails took an effort to eliminate this disparity through  
HashWIthIndiferentAccess, which will take :key or ‘key’ and make sure it  
points to the same value.

Rails 1.9 now includes the fix:

:symbol.is\_a? String =\> true

[http://redhanded.hobix.com/inspect/SymbolIs\_aString.html](http://redhanded.hobix.com/inspect/SymbolIs_aString.html)

Hope that helps.

Jason

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 7, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919/4 "2006-09-07T22:46:07Z")

</div>

On 9/7/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> v.class.name, I get HashWithIndifferentAccess.
> 
> WTF? Is it unreasonable for me to expect the value of a params hash  
> element to be String?

Params in rails can be multi level; that is, on the rhtml page they  
may b e something along the lines of "foo[bar][baz][bleh]=10, which  
gets translated to the params value as a hash\*. And, I think because  
you can use symbols or strings as the Hash keys, is why it’s  
HashWithIndifferentAccess.

If the value was always a scalar string, you wouldn’t be able to new()  
objects with params. (Well, you could if you chose another  
serialization scheme than turning it into a hash, but a hash seems  
reasonable.)

- If I’m recalling correctly, the hash for the above would be  
something along the lines of params = {:foo =\>{:bar =\> {:baz =\>{:bleh  
=\> 10}}}}

–  
If you stick a screwdriver in your eye don’t expect the manufacturer  
to make you wealthy, instead try being more careful. – Unknown

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 7, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919/5 "2006-09-07T22:52:05Z")

</div>

Rails 1.9??? 🙂

Rails 1.2 or Rails 1.1.9?

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 7, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919/6 "2006-09-07T23:00:08Z")

</div>

On Sep 7, 2006, at 12:41 PM, Wes G. wrote:

> v.class.name, I get HashWithIndifferentAccess.
> 
> WTF? Is it unreasonable for me to expect the value of a params hash  
> element to be String?
> 
> Wes

Hey Wes-

```
I think your problem with this is that in an iteration over the

```

params hash, the v part can also be another hash in order to support  
nested hashes in params. So like this:

> > params = { :foo =\> ‘bar’, :baz =\> {:nik =\> ‘nak’}}  
> > =\> {:baz=\>{:nik=\>“nak”}, :foo=\>“bar”}
> 
> > params.each {|k,v| p v }  
> > {:nik=\>“nak”}  
> > “bar”

```
So you see one of the values in that hash is a string but another is

```

a nested hash. I think this is what is causing you confusion. You  
need to write that method in a recursive way to it traverses the sub  
hashes as well as the top level ones

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 8, 2006, 5:00am UTC](https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919/7 "2006-09-08T05:00:47Z")

</div>

Heh, yeah, Ruby 1.9, and I realize now that I didn’t actually answer the  
question, sorry about that.

Jason

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [September 7, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/param-values-are-hashwithindifferentaccess/67919/8 "2006-09-07T23:14:32Z")

</div>

Jason R. wrote:

> Rails 1.9 now includes the fix:

He meant Ruby 1.9. 🙂
