Article about the previous hash
You can check if the hashes are the same by comparing the hashes with ==. At this time, if all the keys and values are the same, true is returned. True if the keys and values are all the same, even if they are in a different order.
(Example)
x = { 'a' => 1, 'b' => 2, 'c' => 3}
y = { 'b' => 2, 'c' => 3, 'a' => 1}
x == y # => true
You can use the size method (= length) to find out the number of hash elements.
{ 'a' => 1, 'b' => 2, 'c' => 3 }.size # => 3
You can delete the element corresponding to the key specified by the delete method. Finally, if you print the hash, you can see that the element has been removed.
menus = { 'food' => 'rice', 'drink' => 'water', 'dessert' => 'cake' }
menus.delete('food') # => "rice"The value of the deleted element will be the return value
puts menus # => {"drink" => "water", "dessert" => "cake"}
Introduction to Ruby for those who want to become a professional
Recommended Posts