From: Jason Leong Date: 2008-11-03T09:00:15+09:00 Subject: Re: Indexed arrays, delete_if, and performance > Please look carefully at the code. Although untested it should do > exactly this: delete from the other Hash. Note that the return value of > Hash#delete is the deleted element. I probably should have added a > safety check to ensure nil does not cause errors. So you'd probably > rather do > > def delete_by_id(id) > dlt = @ev_by_id.delete(id) and dlt.each do |ev| > @ev_by_date[ev.date].delete(ev) > end > end Ah yes! Pounded off a reply before I looked, sorry - thanks for the safety check too, that certainly came in handy. The one difference in my final implementation is this: def delete_events_by_id(id) dlt = @titles.delete(id.to_i) and dlt.each do |e| @events[e.date].delete_if { |i| i.id == e.id } end end The reason being (I think!) the object passed in for deletion in @ev_by_date[ev.date].delete(ev) does not match up with the object in @ev_by_date as they're two different Hashes - but a compare based on the id works. Does that sound right? Thanks Robert, you've taught me a bunch about Hashes! -- Posted via http://www.ruby-forum.com/.