[For super beginners] The minimum knowledge you want to keep in mind with hashes and symbols

Difference between array and hash

An array is a collection of multiple pieces of information that have the same meaning, and a hash is a collection of multiple pieces of related information.

hash

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

-Since values with different meanings can be combined into one, it is used when managing multiple related values. -Has a set of "data" and the corresponding "name" as an element ・ In hash, data is called value and name is called key.

#If you use a string as the hash key
hash1 = { "name" => "nick", "age" => 30, "country" => "UK" }

symbol

・ What is used as a numerical value for the hash key ・ Although it looks like a character string, the actual content is a numerical value. ・ There are two types of writing, but the actual situation is the same for both. ・ Hash3 is the simplest and most often used

#When using a symbol as the hash key
hash2 = { :name => "nick", :age => 30, :country => "UK" }
hash3 = { name: "nick", age: 30, country: "UK" }

⚠️ Computers process numbers faster than strings Symbols are more commonly used


So far, the super basic part is clear! !! It's a good idea to get used to the hashes written with symbols, but make sure you understand them before using them. Next, I would like to explain about adding hashes and getting double hash values! If you want to absorb a little more knowledge, please see the continuation.


Add element to hash


teacher = { name: "nick"}
teacher[:age] = 30 #hash[Key to add] =value

puts teacher

#Output result
# {:name=>"nick", :age=>30}

Change hash value


teacher = { name: "nick", age: 30}
teacher[:name] = "john" #hash[The key of the value you want to change] =value

puts teacher

#Output result
# {:name=>"john", :age=>30}

Get the hash value


teacher = { name: "nick", age: 30}
puts teacher[:name] #hash[The key of the value you want to get]

#Output result
# nick

Get the value of the double hash

#Variable teacher_data → Have multiple pieces of information as hashes inside the array
#teacher_Get name data from data

teacher_data = [
  {
    teacher: {
      profile: {
        name: "nick"
      }
    }
  },
  {
    teacher: {
      profile: {
        name: "john"
      }
    }
  },
  {
    teacher: {
      profile: {
        name: "mac"
      }
    }
  }
]

teacher_data.each do |t|
  puts t[:teacher][:profile][:name]
end

#Output result
# nick
# john
# mac

[Explanation] The block argument t uses t of teacher_data. After puts, concatenate the hash [key you want to get] to the data you want to get (name in this case).


Thank you for your hard work! !! That's all for super basic knowledge about hashes and symbols! If you don't understand the basics, you'll be in trouble later, so I hope you take this opportunity to deepen your understanding. Please do not hesitate to let us know if you have any questions or concerns! Then thank you.

reference

・ Htps: // Quiita. This m / Ryosukette r / Te ms / 257d672 Eb83210b5f8dc

Recommended Posts

[For super beginners] The minimum knowledge you want to keep in mind with hashes and symbols
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
(For super beginners) Getter / setter and property to think in D language
[For beginners] I want to automatically enter pre-registered data in the input form with a selection command.
[For beginners] DI ~ The basics of DI and DI in Spring ~
[For beginners] Minimum sample to display RecyclerView in Java
If you want to recreate the instance in cloud9
When you want to explicitly write OR or AND with ransack
docker-compose.yml when you want to keep mysql running with docker
Things to keep in mind when using Sidekiq with Rails
How to write when you want to keep line breaks and output while avoiding XSS in Rails
I want to return to the previous screen with kotlin and java!
The story of Collectors.groupingBy that I want to keep for posterity
If you want to include the parent class in Lombok's @builder
[Tips] How to solve problems with XCode and Swift for beginners
[Java] I want to perform distinct with the key in the object
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
[For beginners] When you want to say that the JVM (-D) option does not work with the java -jar command, or that the library is buggy.
Wait for PostgreSQL to start with Docker and then start the WEB service
Glassfish tuning list that I want to keep for the time being
Things to keep in mind when combining if statements and logical operators
Things to keep in mind when using Apache PDFBox® with AWS Lambda
Find the maximum and minimum of the five numbers you entered in Java
[Rails] Articles for beginners to organize and understand the flow of form_with
I want you to use Enum # name () for the Key of SharedPreference
Summary of hashes and symbols in Ruby
[Java Bronze] 5 problems to keep in mind
[For beginners] How to debug in Eclipse
If you want to satisfy the test coverage of private methods in JUnit
I want to control the start / stop of servers and databases with Alexa
Get YouTube video information with Retrofit and keep it in the Android app.
You may not want to use the remove method in ArrayList very often
If you are using Android Room and want to change the column definition
I want you to use Scala as Better Java for the time being
Code used when you want to process Json with only the standard library in Java (improved version) gson unnecessary
[Comma (,) is strictly prohibited in the address! ] Things to keep in mind when applying for an exam at Pearson VUE
Summary of copy and paste commands used when you want to delete the cache in iOS application development anyway
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument