[Complete programming] §3 Let's calculate using Ruby!

Purpose

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

Basic programming grammar

In Last time, the character string typed in VsCode is displayed on the PC. This time, let's deal with cooperation and numerical calculation in addition to character strings!

String concatenation

Let's start with the concatenation of strings! Type the following statement into VsCode.

sample.rb


puts "Hello" + " World" #""(Don't forget the double quotes)

Open the sample.rb directory in your terminal.

$ ruby sample.rb #If it is written as follows, it is successful.
Hello World

Numerical calculation

Next, do the numerical calculation! In programming, operators are used for numerical calculations as follows.

addition +
subtraction -
multiplication *
division /
Multiplying and dividing %

I will calculate using these.

sample.rb


puts 3 + 4
puts 5 - 3
puts 2 * 4
puts 9 / 3
puts 7 % 4

What you should pay attention to here is "" (double quotation) in the numerical value. Do not add "" (single quotation).

Check the calculation result in the terminal.

$ ruby sample.rb #If it is written as follows, it is successful.
7
2
8
3
3

If you get the above result, you are successful!

Concatenation of strings and numbers

Learn how to concatenate strings and numbers!

sample.rb


puts "This apple" + 200 + "It's a yen"

Check the terminal in this state.

$ ruby sample.rb
sample.rb:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input
puts "This apple" + 200 + "It's a yen"
                         ^~~

The details of the error will be explained later. Here, there is no problem in recognizing that the program is not working properly.

So what's wrong? This is ** strings and numbers are different, so you can't concatenate them as they are. ** ** Use the ** to_s ** method to concatenate!

to_s method

The to.s method is a method that converts a number as a character string.

sample.rb


puts "This apple" + 200.to_s + "It's a yen" #.(Dot)Be careful not to forget to attach!

Check the terminal.

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

Comparison operator

If you want to compare numbers, use a comparison operator. When the comparison operator (<,>, =) is used, faulse (false) and true (true) are displayed on the terminal.

sample.rb


puts 1 < 30
puts 1 > 30
puts 3 + 5 == 8
puts 3 + 5 == 7

Check the terminal

$ ruby sample.rb #If it is displayed as below, it is successful.
true
false
true
false

not operator

The! (Exclamation mark) is called the ** not operator ** and is used when you want to express negation.

sample.rb


puts 3 + 5 != 7 #Confirm that 3 + 5 is not 8
puts 6 - 2 != 4 #Confirm that 6-2 is not 4

Check the terminal

$ ruby sample.rb #If it is displayed as below, it is successful.
true
false

that's all! If you are interested in the many operators, please do a search.

Remarks

─────────────────────────────── ■ 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

[Complete programming] §3 Let's calculate using Ruby!
[Programming Encyclopedia] §2 Try using Ruby
[Programming complete] §5 Create a review management app in Ruby
Amazing Java programming (let's stop)
Try using Cocoa from Ruby
DB programming using EclipseLink part1
Ruby Learning # 99 Personal Programming Notebook
Statically typed Ruby using Sorbet
Try using gRPC in Ruby
[Programming Complete] §4 Variables and constants