This is probably more of a ruby question, but I''m posting it here as HashWithIndifferentAccess is more of a rails thing. I have a @morton which is --- !map:HashWithIndifferentAccess new_lab_data_attributes: - !map:HashWithIndifferentAccess unit_id: "4" lab_desc_id: "3" value: "" - !map:HashWithIndifferentAccess unit_id: "2" lab_desc_id: "2" value: "" I''d like to delete the new_lab_data_attributes with value = nil I''ve tried various incarnations of @morton[ :new_lab_data_attributes ].each{ |x| x.delete if x[ :value ] == nil } But nothing seems to be removing those entries. Part of my problem is that I can''t quite figure out the key value pairs in this. It almost seems that the inner !map:HashWithIndifferentAccess are symbols for keys--is that right? Anyway, a method to remove those inner hashes depending on the "value" would be greatly appreciated. TIA, Craig -- 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.
Michael Pavling
2010-Mar-16 14:29 UTC
Re: HashWithIndifferentAccess remove inner hash question
On 16 March 2010 13:54, Dudebot <craignied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is probably more of a ruby question, but I''m posting it here as > HashWithIndifferentAccess is more of a rails thing. > > I have a @morton which is > > --- !map:HashWithIndifferentAccess > new_lab_data_attributes: > - !map:HashWithIndifferentAccess > unit_id: "4" > lab_desc_id: "3" > value: "" > - !map:HashWithIndifferentAccess > unit_id: "2" > lab_desc_id: "2" > value: "" > > I''d like to delete the new_lab_data_attributes with value = nil >none of them are nil - they''re empty strings.... You could try "blank?" (it might work): @morton[:new_lab_data_attributes].each{ |x| x.delete if x[:value].blank? } -- 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.
On Mar 16, 9:29 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You could try "blank?" (it might work): > @morton[:new_lab_data_attributes].each{ |x| x.delete if x[:value].blank? }I''m just lost with this syntax :) delete complains if it doesn''t have an argument, so I tried @morton[:new_lab_data_attributes].each{ |x| x.delete(x) if x[:value].blank? } Which still isn''t deleting anything... -- 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.
Michael Pavling
2010-Mar-16 15:11 UTC
Re: Re: HashWithIndifferentAccess remove inner hash question
On 16 March 2010 15:01, Dudebot <craignied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 16, 9:29 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> You could try "blank?" (it might work): >> @morton[:new_lab_data_attributes].each{ |x| x.delete if x[:value].blank? } > > I''m just lost with this syntax :) delete complains if it doesn''t have > an argument, so I triedIt was your syntax! :-) I just changed "== nil" to ".blank?" Look at the docs for Hash: http://ruby-doc.org/core/classes/Hash.html#M002870 Looks like just dropping the "if" would work... -- 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.
Michael Pavling
2010-Mar-16 15:15 UTC
Re: Re: HashWithIndifferentAccess remove inner hash question
On 16 March 2010 15:11, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Looks like just dropping the "if" would work...*sigh* no it won''t... there''s an extra .each iterator in there.... Have a play with "delete_if" instead of the "each"... -- 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.
On Mar 16, 10:11 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> It was your syntax! :-) I just changed "== nil" to ".blank?" > Look at the docs for Hash:http://ruby-doc.org/core/classes/Hash.html#M002870 > > Looks like just dropping the "if" would work...Oops, you were right about that (I''m so addled by trying different things at this point, that I''m not seeing the obvious) but it still doesn''t work: @morton[:new_lab_data_attributes].each{ |x| x.delete x[ :value ].blank? } raise @morton.to_yaml --- !map:HashWithIndifferentAccess new_lab_data_attributes: - !map:HashWithIndifferentAccess unit_id: "4" lab_desc_id: "3" value: "" - !map:HashWithIndifferentAccess unit_id: "2" lab_desc_id: "2" value: "" -- 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.
On Mar 16, 10:15 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Have a play with "delete_if" instead of the "each"...Thou art a genius! @morton[:new_lab_data_attributes].delete_if{ |x| x[ :value ].blank? } Works. *Many, many* thanks -- 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.
Michael Pavling
2010-Mar-16 16:06 UTC
Re: Re: HashWithIndifferentAccess remove inner hash question
On 16 March 2010 15:24, Dudebot <craignied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 16, 10:15 am, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Have a play with "delete_if" instead of the "each"... > > Thou art a genius! > > @morton[:new_lab_data_attributes].delete_if{ |x| x[ :value ].blank? } >It ain''t me; it''s the documentation... My first port of call is always to type "ruby rails api <my problem>" into Google. But thanks :-) -- 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.