Ruby basics

I will write about the basics of Ruby from now on.

First, I will explain the difference between character strings and numbers.


Hello Hello! Today he calls non-numeric numbers such as Konnichiha strings. When typing '(Single quote) Or "(Double quotation) Enclose in.

For example, "Hello" 'Hello'

Numerical value

Numbers such as 22, 777, and 8. You don't have to enclose it in single or double quotes.

Addition, subtraction, multiplication and division in Ruby

In the case of the string by connecting the Hello door Taro, if you want to hello Taro is And the "Hello" + "Taro"

In the case of numerical values, when you want to add 5 and 9 5 + 9

So what happens if you add a string and a number?

Hello, Today is 24 days. When making the sentence by addition

"Hello, it is today" "The Day." + 24 +

Then I get an error. This is because numbers and strings cannot be calculated directly.

Then, what to do is to convert the numerical value to a character string using the to_s method.

"Hello, today is" + 24.to_s + "is the date." To clean up can be calculated.

On the contrary, if you want to convert the character string to a numerical value, use the to_i method.

"500" If it is surrounded by single quotation marks or double quotation marks, it will be treated as a character string. If you want to convert it to a numerical value, use "500" .to_i.

That's all for today, and next time I'll write about methods.

Recommended Posts

Ruby basics
Ruby basics
Ruby basics
Basics of Ruby
[Ruby] Exception handling basics
Ruby on Rails basics
Ruby learning 4
Rails basics
[Ruby] Array
Ruby learning 5
Basics of sending Gmail in Ruby
Ruby Review 2
Ruby addition
Fragment basics
Refactoring Ruby
JPA Basics 1
Docker basics
Ruby learning 3
ViewPager basics
Java basics
[Ruby basics] split method and to_s method
Java basics
[Ruby] Date, Time, Datetime class basics
RSpec Basics
Ruby setting 2
Ruby problem ⑦
Basics of Ruby ~ Review of confusing parts ~
Ruby learning 2
JavaScript basics
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
JPA Basics 2
Hash basics
Refactoring Ruby
Java basics
Ruby Basics 2 ~ Review of confusing parts ~
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
RecyclerView Basics
[Ruby basics] Methods that frequently use blocks
[Ruby] From the basics to the inject method
Rails CSV basics
Ruby input / output
[Ruby] Summary of class definitions. Master the basics.
ruby Uppercase letters
ruby search problem
Ruby Learning # 25 Comments
ruby constant variable
Ruby to_s method
[Ruby] postfix if
Rails Routing Basics
[Ruby] FizzBuzz problem
Ruby text conversion
Ruby basic terms
Rails database basics
ruby exception handling