Personal memo Progate Ruby I (2)

6. Variable basics

・ Variables: A box that holds values. Variables are defined by "variable name = value".

animal = "Seal"
↳ Variable name ↳ Value to be assigned

[Caution]

・ 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

[Caution]

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.

7. Let's use variables

The program is executed in order from the top

⇒ 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

8. Roles of variables and precautions

● 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.

9. Variable update (1)

● 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.

10. Variable update (2)

● 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.

Application (abbreviation)

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

11. Variable expansion

● Variable expansion

Include variables in strings

#{Variable name} ⇒Variable expansion: How to include the value of a variable in a character string.

[Caution]

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

Personal memo Progate Ruby I (2)
Personal memo Progate Ruby I (1)
ruby exercise memo I (puts)
Ruby memo
I started Ruby
[wip] Ruby memo
[Personal memo] I learned a little about modifiers
[Personal memo] Ruby on Rails environment construction (Windows)
[Personal memo] try-catch summary
ruby basic syntax memo
[Personal memo] I tried to study object orientation lightly
[Personal memo] About Spring framework
I tried DI with Ruby
Ruby study memo (conditional branching)
[Self-use memo] (Ruby) class, class instance, instance
[Swift] Closure basics (personal memo)
Ruby Learning # 99 Personal Programming Notebook
Personal memo Eclipse plug-in installation
Java study memo 2 with Progate
[Personal memo] Number guessing game
Ruby study memo (recursive function)
Personal memo Lombok's typical annotation
I tried Jets (ruby serverless)
I don't understand Ruby 3 Ractor
[Ruby ~ Iterative processing ~] Study memo 4
Java HashMap, entrySet [Personal memo]
[Ruby 3.0] A memo that I added a type definition to a library I wrote
Ruby design pattern template method pattern memo
Completed Progate Ruby Learning Course III
Truffle Tutorial Slides Personal translation memo ①
ruby exercise memo VI (hello class)
Personal memo: Metaprogramming with Java reflection
Progate Ruby on Rails5 Looking Back
Completed Progate Ruby Learning Course II
Ruby Study Memo (Test Driven Development)
Ruby on Rails 6.0 environment construction memo
String output method memo in Ruby
<First post> I started studying Ruby
[Note] [Beginner] Ruby writing memo (refactoring) 1
I tried using Java memo LocalDate