[RUBY] [Programming Complete] §4 Variables and constants


Although I was enthusiastic about "Let's learn programming !!" Programming seems to be difficult, I can't read English, I'm not good at PC operation itself, For you who were frustrated a long time ago __ Read this article ・ Just move your PC and you'll see "What! Programming is interesting!" The purpose is to make you think. __

I would like to serialize it under the title of Programming Encyclopedia.

Development environment

Variables and constants

Today I would like to write about variables and constants. You may be familiar with junior high school mathematics, but the nuances are a little different in programming.

What is a variable?

Variable values are a technique for recognizing objects such as strings and numbers. By using a variable, it is possible to call the object you want to reuse by the name of the variable.

It's hard to imagine if it's just words, so let's check it while actually running the program!


word = "Hello World" #In this case word becomes the variable name"Hello world"Is the value you want to store.
puts word #Type the variable name after puts

Check in the terminal.

 $ ruby sample.rb #If it is displayed as below, it is successful.
Hello World

The characteristic of variables is that they can be reassigned in a program **.


word = "Hello World" 
word = "This apple costs 200 yen." #Change the variable in the line above.
puts word 

Check in the terminal.

 $ ruby sample.rb #If it is displayed as below, it is successful.
This apple costs 200 yen.

As mentioned above, it will be rewritten to the one assigned later. In Ruby, you can assign any object to a variable after assigning a string It is also possible to substitute a numerical value. You can also use methods on variables.

Practice working with variables

Let's deal with variables a little more to make it easier to imagine.

Write a program to find "the area of a triangle with a base of 3 and a height of 8".


bottom = 3 #The base is a variable.
puts bottom * 8 / 2

Check in the terminal.


So what if the base of this triangle is 5?


bottom = 5 #Just return the value here to 5
puts bottom * 8 / 2

Check in the terminal.


Try to write easy-to-understand code using variables like this!


Finally, constants are basically a way to store values like variables. The difference with variables is ** reassignment is not possible. ** ** Defining a constant is ** capitalizing the first letter. ** **

When I try to reassign a constant, I get the following error statement.


WORD = "Hello world"
WORD = "This apple costs 200 yen."
puts WORD
$ ruby sample.rb
sample.rb:2: warning: already initialized constant WORD
sample.rb:1: warning: previous definition of WORD was here
This apple costs 200 yen.

For constants, use values that do not change, such as pi!


PI = 3.14
puts 5 * 5 * PI
$ ruby sample.rb #If it is displayed as below, it is successful,

that's all!


─────────────────────────────── ■ Books recommended by the author ───────────────────────────────

"Introduction to Web Technology to Become a Professional"

"How to think about changing jobs"
"High power marketing"
"Courage to be disliked"
"Complete output"

─────────────────────────────── ■ Movies recommended by the author ───────────────────────────────

"My Intern"
"Shin Godzilla"
"Dragon Ball Super Broly"
「School of Roc」

Recommended Posts

[Programming Complete] §4 Variables and constants
[Swift] Constants and variables
[Programming Complete] §4 Variables and constants
Java programming (static clauses and "class variables")
[Java] Variables and types
Difference between variables and instance variables
Programming and high school math
Ruby variables and functions (methods)
About instance variables and attr_ *
Java Primer Series (Variables and Types)
Organize classes, instances, and instance variables
[Complete programming] §3 Let's calculate using Ruby!