・ Variables: A box that holds values. Variables are defined by "variable name = value".
animal = "Seal"
↳ Variable name ↳ Value to be assigned
・ Programming "=" ⇒ It does not mean "equal".
・ Then, what is the "=" in programming? ⇒ It means "put the right value in the left variable". This flow is called "assignment".
index.rb replacement image
animal = "Seal"      →  animal =  "Seal" 
puts animal              →  puts "Seal" 
↳ Variable animal is the value("Seal")Replace with
>_console
Seal
animal="Seal"                    >_console
                
*1.puts animal seal
*2.puts "animal"                      animal
If you enclose the variable in double quotes like * 2, Since it is recognized as a character string, it is not enclosed when it is treated as a variable.
⇒ When using a variable, be sure to define the variable name.
● Variable containing a character string
-Calling a variable "replaces" the assigned value -Variables containing character strings can be handled in the same way as character strings.
message ="I"           
puts message + "It's a seal" 
animal = "It's an otter"
puts message + animal
>_console
I'm a seal
I'm an otter
● Variable with numerical value
    number1 = 20                        
 puts number1 + 3                             
     number2 = 15                                  
 puts number1 + number2 
>_console
23
38
● Why use variables? -There are many values in the program. ・ Accuracy and flexibility are required when writing programs. ⇒ One of the mechanisms to solve these two points is a variable.
● Role of variables ⇒The same element can be used repeatedly ⇒ Easy to respond to changes ⇒ Easy to understand what the elements are
・ When variables are not used
index.rb
puts "I'm a harbor seal"
puts "I'm a bearded seal"
puts "I'm a harp seal"
puts "I'm a Baikal seal"
If you replace the "I" part with "Actually I" All three must be changed.
・ When using variables
index.rb
text = "Actually I"
puts = text + "It ’s a harbor seal." 
puts = text + "It ’s a bearded seal."
puts = text + "It ’s a harp seal."
puts = text + "Baikal seal"
Substitute if you replace the "I" part with "Actually I" All you have to do is change the string.
● Easy-to-understand variable names ⇒ What value is assigned to the variable Give a variable name that can be judged
○ animal = "Seal"
×   I am = "Seal"
● Variable name rules ⇒ Ruby has some naming rules.
[Caution] When assigning a variable name that combines two or more words, Use underscores (_).
Ex. Spotted_seal etc.
● Change the value of a variable
Variables can change the value once assigned. ⇒ If you assign a value to a variable that has been assigned a value once, then assign the value again. The contents are overwritten.
 index.rb        >_console
 number = 1                 
 puts number             1
 number = 7
 puts number             7
⇖ The value of the overwritten variable is output.
⇒Since the program is executed in order from the top, the contents will be overwritten.
● Substitute for yourself
When "I want to add 3 to the value of the variable number that has already been defined" What should i do?
Answer:
                                  >_console
number = 2
puts number                2
number = number + 3
puts number                            5
Add 3 to number as in the code above and assign it to number again. The number to the right of "=" is replaced with the value, and the calculated result is assigned to the number on the left.
⇒Similarly when assigning to yourself, the program is in order from the top Since it is executed, the contents are overwritten.
Uninflected abbreviation
x = x + 7                      x += 7
x = x -  7                     x - = 7
x = x * 7                      x * = 7
x = x / 7                      x / = 7
x = x % 7                      x % = 7
● Variable expansion
Include variables in strings
#{Variable name} ⇒Variable expansion: How to include the value of a variable in a character string.
I used double quotes Variable expansion is possible only for strings. ⇒ Single quotation is NG
"" = 〇 
'' = ×
ex.In the case of otters
animal = "Otters"
puts "Hello#{name}Mr."
>_On the console
Hello otter's
● Advantages of variable expansion:
index.rb(☆)
age = 25
puts age + "I'm old"
   (Numerical value) (String)
---------------------
>_On the console
× Error occurred!!
--------------------------------------------------------
index.rb(★)
age = 25
puts "#{age}I'm old"
---------------------
>_On the console
25 years old
● Variable expansion
Commentary: Numerical values and character strings like ☆ It cannot be connected by addition. However, if you use variable expansion like ★ Variables with numbers can be converted to strings without any problem Can be included. ⇒ If you want to include variables in the string Basically use variable expansion.
Recommended Posts