HashWithIndifferentAccess remove inner hash question

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

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

I'm just lost with this syntax :slight_smile: 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...

It was your syntax! :slight_smile: 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...

*sigh* no it won't... there's an extra .each iterator in there....

Have a play with "delete_if" instead of the "each"...

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: ""

Thou art a genius!

@morton[:new_lab_data_attributes].delete_if{ |x| x[ :value ].blank? }

Works. *Many, many* thanks

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 :slight_smile: