About Ruby Hashes

Article on previous hash

hash

A hash is an object that manages data with a combination of keys and values.

If you want to create a hash, use a syntax like this:

{Key 1 => Value 1, Key 2 => Value 2, Key 3 => Value 3}

Example of actually creating a hash

{'food' => 'rice', 'fruit' => 'lemon'} 

Add element

If you want to add a new key and value after creating the hash, use the following syntax.

Hash [key] = value

(Example) The following is the code to add a new drink type.

menu = { 'food' => 'rice', 'fruit' => 'lemon',  }

#Add a drink menu
menu['drink'] = 'water'

puts menu #=> {"food" => "rice", "fruit" => "lemon", "drink" => "water"}

Overwriting elements

If the key already exists, the value will be overwritten.

menu = { 'food' => 'rice', 'fruit' => 'lemon',  }

#Overwrite the value
menu['food'] = 'pizza'

puts menu #=> {"food" => "pizza", "fruit" => "lemon"}

Iterative processing using hashes

You can use the each method to retrieve a set of keys and values in sequence.

menu = { 'food' => 'rice', 'fruit' => 'lemon',  }

menu.each do |key, value|
  puts "#{key} : #{value}"
end

# => food : rice
#    fruit : lemon

When using each method for hash, specify "key" and "value" for variables. |variable|のvariableはブロック引数といい、eachメソッドから渡されたハッシュの要素が入る。 The "key" of the hash is assigned to the variable key, and the "value" is assigned to the variable value in order. Then, after the process described in the block is repeatedly executed for the number of hash elements, the process of exiting the block ends.

Referenced literature

Introduction to Ruby for those who want to become professionals

Recommended Posts

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 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 =
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 object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
About method.invoke
Ruby learning 4
About Kotlin
[Ruby] Array
About Hinemos
Ruby basics
About inheritance
About params
About Docker
Ruby Review 2
Ruby addition
Refactoring Ruby
About Rails 6
About form_for
[Ruby on Rails] About bundler (for beginners)
Ruby learning 3
About enum
About polymorphism
About Optional
About JitPack
About Dockerfile