John Merlino
2011-Oct-17 21:27 UTC
adding key/value pairs to hash wrongfully adds a right bracket
Hey all, THis line of code gets called multiple times and creates a hash: def session_code(unit_id, code) s_code = session[:code] unit_id_hash = s_code.detect {|h| h[unit_id]} if unit_id_hash.nil? unit_id_hash = {} s_code << {unit_id => unit_id_hash} end key = case code when 3,4 :code_a when 8,9 :code_b end unit_id_hash[key] ||= [] unit_id_hash[key] << code if key end But the result looks like this: [{"1113"=>{:code_a=>[8]}, :code_b=>[4]}] There is an extra "}" in that hash after [8]. THat''s strange why that happens. The result should look like this: [{''1113'' => {:code_a => [8], :code_b => [4]}}] Because of the problem I cannot grab the second hash value: session[:code].detect {|hash| hash["1113"] }["1113"][:code_b] thanks for response -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tim Shaffer
2011-Oct-18 00:58 UTC
Re: adding key/value pairs to hash wrongfully adds a right bracket
How do you call the session_code method? What data type are unit_id and code? And what does a typical session[:code] look like? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/qoZeiGK8qkgJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Oct-18 10:26 UTC
Re: adding key/value pairs to hash wrongfully adds a right bracket
On Oct 17, 10:27 pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote:> Hey all, > > THis line of code gets called multiple times and creates a hash: > > def session_code(unit_id, code) > > s_code = session[:code] > unit_id_hash = s_code.detect {|h| h[unit_id]} > > if unit_id_hash.nil? > unit_id_hash = {} > s_code << {unit_id => unit_id_hash} > endThe two branches of this code result in unit_id_hash looking somewhat different . In the case where you find something in s_code, what you''re setting unit_id_hash to is something of the form {unit_id => {...}} whereas I expect you''re expect it to be the value from that hash for the key unit_id Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2011-Oct-19 15:08 UTC
Re: adding key/value pairs to hash wrongfully adds a right bracket
thanks for response. I see what you are trying to say, but this line right here: s_code << {unit_id => unit_id_hash} it just creates a hash like this: {unit_id => {}} and then latter we insert items into it, always the same way, whether it''s the first time we insert item or not: unit_id_hash[key] << code if key So if you see a problem here, maybe I am not understanding. thanks for response On Oct 18, 6:26 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 17, 10:27 pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote: > > > Hey all, > > > THis line of code gets called multiple times and creates a hash: > > > def session_code(unit_id, code) > > > s_code = session[:code] > > unit_id_hash = s_code.detect {|h| h[unit_id]} > > > if unit_id_hash.nil? > > unit_id_hash = {} > > s_code << {unit_id => unit_id_hash} > > end > > The two branches of this code result in unit_id_hash looking somewhat > different . > > In the case where you find something in s_code, what you''re setting > unit_id_hash to is something of the form {unit_id => {...}} whereas I > expect you''re expect it to be the value from that hash for the key > unit_id > > Fred-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Oct-19 15:18 UTC
Re: Re: adding key/value pairs to hash wrongfully adds a right bracket
On 19 October 2011 16:08, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote:> thanks for response. I see what you are trying to say, but this line > right here: > > s_code << {unit_id => unit_id_hash} > > it just creates a hash like this: > {unit_id => {}}Open a rails console and enter code that illustrates the problem. Then copy/paste that here. If it seems to work in the console then the contents of your variables may not be what you expect. Use ruby-debug to break into the code, then you can inspect the data and follow the flow to see what is going wrong. Have a look at the Rails Guide on debugging to see how to use ruby-debug if necessary. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dave Aronson
2011-Oct-23 19:19 UTC
Re: adding key/value pairs to hash wrongfully adds a right bracket
On Mon, Oct 17, 2011 at 17:27, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote:> THis line of code gets called multiple times and creates a hash:Before I try to figure it out further, something smells. You pass in an arg called "code", and often refer to ":code". This sort of "shadowing" is Very Confusing. If they are supposed to be the same thing, you''ve got some colons to remove (or maybe add and an arg to dump), else you''ve got an arg to rename. -Dave -- LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages. Where: Northern Virginia, Washington DC (near Orange Line), and remote work. davearonson.com (main) * codosaur.us (programing) * dare2xl.com (excellence) Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2011-Oct-29 03:31 UTC
Re: adding key/value pairs to hash wrongfully adds a right bracket
thanks for response, code was an integer, and :code was just a key of the session hash. But I decided in end to make this database backed. On Oct 23, 3:19 pm, Dave Aronson <googlegroups2d...-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> On Mon, Oct 17, 2011 at 17:27, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote: > > THis line of code gets called multiple times and creates a hash: > > Before I try to figure it out further, something smells. > > You pass in an arg called "code", and often refer to ":code". This > sort of "shadowing" is Very Confusing. If they are supposed to be the > same thing, you''ve got some colons to remove (or maybe add and an arg > to dump), else you''ve got an arg to rename. > > -Dave > > -- > LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages. > Where: Northern Virginia, Washington DC (near Orange Line), and remote work. > davearonson.com (main) * codosaur.us (programing) * dare2xl.com (excellence) > Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.