About Ruby Hashes (continued)

Article about the previous hash

Equivalence comparison of hashes

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.


x = { 'a' => 1, 'b' => 2, 'c' => 3}
y = { 'b' => 2, 'c' => 3, 'a' => 1}
x == y # => true

Get the number of elements

You can use the size method (= length) to find out the number of hash elements.

{ 'a' => 1, 'b' => 2, 'c' => 3 }.size # => 3

Delete element

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"}

Referenced literature

Introduction to Ruby for those who want to become a professional

Recommended Posts

About Ruby Hashes (continued)
About Ruby Hashes
About Ruby Hashes
About hashes
About Ruby hashes and symbols
About Ruby symbols
About ruby ​​form
Ruby Learning # 14 Hashes
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Symbols
About Ruby variables
About Ruby methods
About Ruby Kernel Module
About Ruby error messages
About Ruby exception handling
About eval in Ruby
[ruby] About here documents
About Ruby if statement
About Ruby instance methods
[Ruby] About instance generation
About the [ruby] operator
Thinking about logic Ruby
Basic methods of Ruby hashes
What you learned about hashes
Explanation about Ruby Range object
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
Ruby About various iterative processes
About Ruby and object model
About Ruby classes and instances
Explanation about Ruby String object
About the behavior of ruby Hash # ==
Sorting hashes in a Ruby array
About Ruby single quotes and double quotes
About Ruby product operator (&) and sum operator (|)
[Super Introduction] About Symbols in Ruby
About =
About object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
[Ruby on Rails] About bundler (for beginners)
Summary of hashes and symbols in Ruby
[Ruby on Rails] About Active Record callbacks
Continued Talk about writing Java in Emacs @ 2018