How to add a new hash / array

Introduction

I've been using the Rails framework these days, so I'll recap the early learning content that I just forgot.

Add new values and keys to the hash

hash = {}

hash[:name] = "I did it" #New keys not in the hash
hash[:name] = "Kanemura" #The value of the existing key is updated

puts hash[:name] #When outputting, the key name is a symbol in square brackets
hash = {name: "Kanemura"} #The final hash looks like this

Array

arrays = []
hash = {name: "Kanemura"}

arrays << hash #Add to the array in hash form
puts arrays[0][:name]
#=>"Kanemura"
puts arrays
#=>{:name=>"Kanemura"}

Finally

When I was using Rails, I wondered if I was using hashes inside, but I completely forgot how to handle it in Ruby.

Recommended Posts

How to add a new hash / array
[Rails] How to add new pages
How to make a Java array
How to add the same Indexes in a nested array
[Xcode] How to add a README.md file
How to create pagination for a "kaminari" array
How to add a classpath in Spring Boot
How to use an array for a TreeMap key
How to add ActionText function
How to convert a file to a byte array in Java
How to initialize Java array
How to insert a video
How to add HDD to Ubuntu
How to create a method
[Java] How to start a new line with StringBuilder
How to divide a two-dimensional array into four with ruby
How to retrieve the hash value in an array in Ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
[Ruby] How to retrieve the contents of a double hash
How to add elements without specifying the length of the array
How to output array values without using a for statement
How to add Hyperledger Iroha Peer
How to make a Java container
How to sign a Minecraft MOD
How to make a JDBC driver
[Java] How to create a folder
How to write a ternary operator
[Swift] How to send a notification
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
How to add the delete function
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to execute a contract using web3j
How to add local jar to maven pom.xml
How to sort a List using Comparator
How to make a Java calendar Summary
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
[Basic] How to write a Dockerfile Self-learning ②
How to insert a video in Rails
[Java] How to add data to List (add, addAll)
How to add jar file in ScalaIDE
[Introduction to Java] How to write a Java program
How to create a Maven repository for 2020
How to make a Discord bot (Java)
How to print a Java Word document
[Swift5] How to create a splash screen
[rails] How to create a partial template
How to publish a library in jCenter
How to add application version information to Sentry information
[SpringBoot] How to write a controller test
[IntelliJ IDEA] How to automatically add final when saving a Java file
[Java] How to turn a two-dimensional array with an extended for statement
How to give MAX + 1 ID to registered data when adding a new record
[Rails] How to create a table, add a column, and change the column type
[Java] [For beginners] How to insert elements directly in a 2D array
Rails: How to write a rake task nicely
How to create a database for H2 Database anywhere
[Rails] How to write when making a subquery
How to deploy a container on AWS Lambda