1

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

1
  • You're asking about an array, not a hash, right? Commented Feb 4, 2011 at 2:54

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.