# HashWithIndifferentAccess remove inner hash question

**URL:** <https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233>\
**Category:** Rails\
**Created:** [March 16, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233 "2010-03-16T14:55:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 16, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233/1 "2010-03-16T14:55:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 16, 2010, 3:33pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233/2 "2010-03-16T15:33:06Z")

</div>

On 16 March 2010 13:54, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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? }

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 16, 2010, 4:08pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233/3 "2010-03-16T16:08:13Z")

</div>

On Mar 16, 9:29 am, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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…

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 16, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233/4 "2010-03-16T16:26:57Z")

</div>

On 16 March 2010 15:11, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”…

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 16, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233/5 "2010-03-16T16:17:00Z")

</div>

On 16 March 2010 15:01, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mar 16, 9:29 am, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

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](http://ruby-doc.org/core/classes/Hash.html#M002870)

Looks like just dropping the “if” would work…

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 16, 2010, 4:32pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233/6 "2010-03-16T16:32:49Z")

</div>

On Mar 16, 10:15 am, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 16, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233/7 "2010-03-16T17:16:07Z")

</div>

On 16 March 2010 15:24, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mar 16, 10:15 am, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 ”  
into Google.

But thanks 🙂

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 16, 2010, 4:28pm UTC](https://www.ruby-forum.com/t/hashwithindifferentaccess-remove-inner-hash-question/185233/8 "2010-03-16T16:28:07Z")

</div>

On Mar 16, 10:11 am, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It was your syntax! 🙂 I just changed “== nil” to “.blank?”  
> Look at the docs for Hash:[class Hash - RDoc Documentation](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: “”
