A hash is a value that has data and a corresponding set of names as elements. For example
puts hash.keys
puts hash.values
When executing each method
puts hash.keys
#Output in terminal
one
two
three
puts hash.value
#Output in terminal
1
2
3
I can display it like How should I describe it?
In hashing, the data is called the value and the corresponding name is called the key. The hash declaration is
#Generate using curly braces
variable= {}
#Hash generation can be generated using a hash rocket
variable= {Key 1=>Value 1,Key 2=>Value 2,Key 3=>Value 3}
In other words
hash = { one => 1, two => 2, three => 3 }
By generating as, you can output as above.
In addition, the value of symbol is often used for hash. The contents of the symbol are numerical values. To declare a symbol, add a colon: at the beginning of the string. Hash keys often use symbols rather than strings.
hash = { :name => "Taro" }
hash = { name: "Taro" }
#Both have the same element
The first is that the key represents a string The second and third can use two notations when the key is a symbol.
hash = { "one" => 1, "two" => 2, "three" => 3 }
hash = { :one => 1, :two => 2, :three => 3 }
hash = { one: 1, two: 2, three: 3 }
You can write as above.
Recommended Posts