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
On 16 March 2010 13:54, Dudebot [email protected] 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? }
On Mar 16, 9:29 am, Michael P. [email protected] 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…
On 16 March 2010 15:11, Michael P. [email protected] 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”…
On 16 March 2010 15:01, Dudebot [email protected] wrote:
On Mar 16, 9:29 am, Michael P. [email protected] 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
Looks like just dropping the “if” would work…
On Mar 16, 10:15 am, Michael P. [email protected] 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
On 16 March 2010 15:24, Dudebot [email protected] wrote:
On Mar 16, 10:15 am, Michael P. [email protected] 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
On Mar 16, 10:11 am, Michael P. [email protected] wrote:
It was your syntax! I just changed “== nil” to “.blank?”
Look at the docs for Hash:class Hash - RDoc Documentation
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: “”