1

I have a hash like this.

h = {1 => 2, 2 => 3, 5 => 8, 4 => 2, 3 => 3} 

I want to delete items whose key is bigger than 3 like this:

h.each{|k,v| p "delete this" if k > 3} 

How can I delete an item inside a loop? Or is there better way to delete an item with a condition?

1 Answer 1

3
h.each{|k,_| h.delete(k) if k > 3} 

or

h.delete_if{|k,_| k > 3} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.