[RUBY] Hash basics

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

puts hash.value
#Output in terminal

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

Hash basics
Rails basics
Ruby basics
Ruby basics
Fragment basics
JPA Basics 1
Docker basics
ViewPager basics
Java basics
RSpec Basics
JavaScript basics
JPA Basics 2
Java basics
Ruby basics
RecyclerView Basics
Rails CSV basics
Rails Routing Basics
Rails Logger Basics
java programming basics
Ruby Hash notes
[Ruby] Hash retrieval
[About double hash]
Basics of Ruby
Java JAR basics
[ruby] Double hash
Object-oriented (Java) basics
vim (gitbush) basics
Rspec Basics [Rails]
Ruby double hash
Stream API basics