Ruby delete_if block
This is using Ruby 1.9.
I have a delete_if block:
@logHash[date].delete_if{ | logItem | logItem.name == name }
Where logItem.name
is the name of the logItem
and name
is the name of the logItem
I'm looking for. This works fine, except it deletes each logItem
with the specif开发者_如何学JAVAied name
. Is there a way to find the first item with an equal name
and only delete that? So if there are two logItems that have the same name
, I only want to delete one of them. Any ideas?
I'd use Array#index
to find the index of the first item which had a matching name, and then Array#delete_at
to delete it.
index_to_delete = @logHash[date].index {|log_item| log_item.name == name}
@logHash[date].delete_at(index_to_delete)
精彩评论