[Understanding] Differences between hashes and arrays in Ruby

Introduction

I was learning ruby and could only understand the hashes and arrays that I often see, but I've finally come to understand it recently, so I summarized it! You can see the difference between hashes and arrays by looking at this! (Perhaps)

First of all, the array

What is an array?

name = ["Suzuki","Tanaka","Onishi"]

--Multiple elements are lined up --Enclosed in [] --Elements are separated by , --Subscripts are specified as integers in order from the front (in the above, name (1) =>" Tanaka ")

What is a hash?

Hash is like this

name = { a: "Suzuki",b: "Tanaka",c: "Onishi" }

--Hash is essentially the same as an array --Enclosed in {} -** You can specify a key (subscript) other than an integer! ** It may be exhausted (in the above, name [: a] =>" Suzuki ")

Why use {} when they are essentially the same

If the hash is surrounded by [], it's easy to lose track of what is the attribute and what is the key, so it's the hash that's surrounded by {}! It seems that it is used to make it easier to understand!

If you know this, it will be easier to distinguish when studying or reading the code!

that's all~

Recommended Posts

[Understanding] Differences between hashes and arrays in Ruby
Differences between Ruby syntax error statements in Ruby and binary
Summary of hashes and symbols in Ruby
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Differences between Ruby strings and symbols [Beginner]
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
About the difference between classes and instances in Ruby
In fact, Ruby distinguishes between line breaks and whitespace.
Differences between Fetch Type LAZY and EAGER in Hibernate
About Ruby hashes and symbols
Differences between IndexOutOfBoundsException and ArrayIndexOutOfBoundsException
[Ruby] Find numbers in arrays
About the difference between "(double quotation)" and "single quotation" in Ruby
Think about the differences between functions and methods (in Java)
Differences in how to handle strings between Java and Perl
[Ruby] How to get the value by specifying the key. Differences between hashes, symbols and fetch
[Ruby] Difference between get and post
Sorting hashes in a Ruby array
Differences between "beginner" Java and Kotlin
[Ruby] Difference between is_a? And instance_of?
[Ruby] then keyword and case in
Write keys and values in Ruby
Differences between Applet class and JApplet class
Understanding equals and hashCode in Java
Differences between Java and .NET Framework
Ruby arrays, hashes, and symbols are confusing, so I summarized them.
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
Differences in writing in Ruby, PHP, Java, JS
A note on the differences between interfaces and abstract classes in Java
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
[Java] Differences between instance variables and class variables
Difference between getText () and getAttribute () in Selenium
[Ruby] Classification and usage of loops in Ruby
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
[Ruby] Difference between print, puts and p
Difference between int and Integer in Java
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
Differences between Spring Initializr packaging JAR and WAR
Difference between next () and nextLine () in Java Scanner
Put CSV files containing "'" and "" "in MySQL in Ruby 2.3
Differences in writing Java, C # and Javascript classes
Summarize the differences between C # and Java writing
Enumerate subsets of arrays given in Ruby (+ α)
[Ruby] Difference between puts and return, output and return value
Distinguish between positive and negative numbers in Java
Toward understanding of map and flatmap in Stream (1)
Ruby How to convert between uppercase and lowercase
Just put `clang 12.0.0` in macOS` Catalina` and put `ruby 3.0.0`
Ruby Learning # 13 Arrays
About Ruby Hashes
Ruby Learning # 14 Hashes
Class in Ruby
About Ruby arrays
About Ruby Hashes
Heavy in Ruby! ??
Ruby and Gem