About Ruby hashes and symbols

Programming study diary

July 9, 2020 Progate Lv.162 RubyⅠ RubyⅡ Start studying Ruby today. I will study so that I can make an application using Ruby on Rails in a month.

What is Ruby

Since Qiita will touch on Ruby for the first time, I will briefly explain Ruby and how to use it first. Ruby is a programming language for creating systems for web applications, and similar languages include PHP and Python. Before getting into the hash of the subject, I will briefly explain how to use Ruby.

Variable definition

Variables can be defined with variable name = value. The basic usage is the same as other programming languages. Make the variant name easy to understand what value is assigned. Use underscore _ for variable names that combine two or more words. </ font>

Variable expansion

By using # {variable name} in the character string, replacing the variable with the assigned value and including the character string is called variable expansion. When expanding variables, be sure to enclose them in double quotation marks " . </ Font> In the case of single quotation marks, variable expansion is not performed and it is treated as a character string as it is. Since the character string cannot be concatenated with +, variable expansion is used even in such a case.

index.rb


name="Tanaka"
puts "Name is#{name}is"
puts 'Name is#{name}is'

console


The name is Tanaka
Name is#{name}is

How to write an if statement

Enclose the process in ʻif and ʻend. In C language, ʻelse if is used, but in Ruby, ʻelse if is used.

Iterative processing

Use each statement.Array.each do |Variable name|And writeendWrite the process you want to execute in the meantime.

index.rb


names=["Tanaka","Yamamoto","Yamada"]
names.each do |name|
  puts name
end

console


Tanaka
Yamamoto
Yamada

The variable name (name) in each statement can be any name you like, but it is often in the singular form of the variable name (names) of the array.  |variable|Can only be used from do to end of each statement.each文の外で定義したvariableはeach文の中でも使うことができる。それぞれのvariableの使用できる範囲をscopeThat is.

What is a hash?

One way to manage multiple values. Arrays manage multiple values side by side, while hashes manage each value with the name keep. 0709.png

index.rb


user={"name"=>"Tanaka", "age"=>16}
puts user
user={"name"=>"Tanaka", "age"=>16}
(↑ The defined hash is output as it is)

Use hash elements

The value of each element of the hash is used by using the corresponding key as a hash [key]. It can also be updated with hash [key] = value. It can also be added with hash [new key] = value.

index.rb


user={"name"=>"Tanaka", "age"=>16}
puts user["name"]

#Key"age"Update value for
user["age"]=17
puts user

#Key"gender"Added value for
user["gender"]=male
puts user

console


Tanaka
user={"name"=>"Tanaka", "age"=>17}
user={"name"=>"Tanaka", "age"=>17, "gender"="male"}

symbol

How to write the key part of the hash with a colon : at the beginning instead of a string. That is, instead of enclosing it in quotation marks, prefix it with a colon. The output result is the same when it is enclosed in quotation marks and when it is prefixed with a colon. Strings and symbols are strictly different, but basically they can be used in the same way. If you use a symbol in the key part of the hash, you must specify it with the symbol when using that value.

index.rb


user={"name"=>"Tanaka", "age"=>17}
puts user["name"]

user={:name=>"Tanaka", :age=>17}
puts user[:name]

[General] </ font> When using a symbol for the hash key, it can be omitted. This way of writing is common. Even if omitted, the element is acquired using the symbol.

index.rb


user={name:"Tanaka", age:17}
puts user[:name]

Treatment of nil

The value of "nothing" when the value of a key that does not exist is extracted from the hash. The reading is "Nil". Nothing is displayed when I puts.

index.rb


user={name:"Tanaka", age:17}
puts user[:height]

An array whose elements are hashes

You can use hashes for the elements of the array. Since a hash can be used in the array [index number], the value of the hash element can be used in the variable [key] using the variable to which the hash is assigned.

index.rb


users=[
  {name:"Tanaka", age:17}
  {name:"Yamamoto", age:20}
]
puts users[0]

user=users[0]
puts user[:name]

console


{:name=>"Tanaka", :age=>17}
Tanaka

The above can be omitted. A specific hash value can be used in the array [index number] [key].

index.rb


users=[
  {name:"Tanaka", age:17}
  {name:"Yamamoto", age:20}
]

puts users[1][:name]

console


Yamamoto

You can also write using each statement.

index.rb


users=[
  {name:"Tanaka", age:17}
  {name:"Yamamoto", age:20}
]

users.each do |user|
  puts user
end

users.each do |user|
  puts user[:name]
end

console


{:name=>"Tanaka", :age=>17}
{:name=>"Yamamoto", :age=>20}
Tanaka
Yamamoto

Recommended Posts