Param values are HashWithIndifferentAccess?

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

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

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

Hope that helps.

Jason

On 9/7/06, Wes G. [email protected] 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

Rails 1.9??? :slight_smile:

Rails 1.2 or Rails 1.1.9?

WG

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

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

Jason

Jason R. wrote:

Rails 1.9 now includes the fix:

He meant Ruby 1.9. :slight_smile: