Ruby/Hash/delete
Материал из Wiki.crossplatform.ru
Содержание |
Delete element from hash
myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five" } myHash.delete( 5 ) # => "Five" p myHash # => {1=>"One", 2=>"Two", 3=>"Three", 4=>"Four"}
Deleting and Clearing a Hash
# You can delete key-value pairs from a hash with the delete method. myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 =>"Four", 5 => "Five" } # I"ll delete the pair identified by key 5: myHash.delete( 5 ) # => "Five"
Deleting Hash Elements
x = { "a" => 1, "b" => 2 } x.delete("a") puts x.inspect
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" }