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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 Gamble <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Wes Gamble 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 9/7/06, Wes Gamble <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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?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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Sep 7, 2006, at 12:41 PM, Wes Gamble wrote:> > 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? > > WesHey 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Rails 1.9??!!!???? :) Rails 1.2 or Rails 1.1.9? WG -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> Rails 1.9 now includes the fix:He meant Ruby 1.9. :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Heh, yeah, Ruby 1.9, and I realize now that I didn''t actually answer the question, sorry about that. Jason On 9/7/06, Daniel Waite <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Jason Roelofs wrote: > > Rails 1.9 now includes the fix: > > He meant Ruby 1.9. :) > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---