pass a block to your call to delete. : delete « Hash « Ruby






pass a block to your call to delete.


If the key you're wanting to delete is not found, the block runs, and its return value will be returned by delete.

myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five" }

myHash.delete( 6 ) { |key| puts "not found, bubba" }

 








Related examples in the same category

1.Deleting Hash Elements
2.Delete element from hash
3.Deleting and Clearing a Hash