This will be a memo for learning.
Originally, it is a mechanism prepared by Unix-like OS such as LINUX. If you create a program that supports standard input, you can switch the input destination, such as reading a file, reading data from the keyboard, or specifying parameters when the program is executed.
One line input from standard input
line = gets
puts line
Read one line from standard input and convert to integer
line = gets.to.i
puts line
//The entered number is output
example)
Two integers are given in two lines on standard input.
Create a program that outputs line by line while increasing the number from the first number to the second number by one. For example, given the numbers 3 and 5, the output would be:
3
4
5
num1 = gets.to_i
num2 = gets.to_i
for i in num1..num2
puts i
end
Convert data to a string
Converting data to a decimal point
Remove the line feed code at the end of the string
line = gets.chomp
puts = "#{line}Attacked the slime" //Output with the line feed code removed
The split method is simply a method for splitting a string into an array.
str = "samurai engineer blog"
array = str.split
p array
[Execution result]
["samurai", "engineer", "blog"]
String.split(Delimiter)
str = "samurai,engineer,blog"
array = str.split(",")
p array
[Execution result]
["samurai", "engineer", "blog"]
The "times" method changes the variable from "0" to "the numerical value of the target object".-While substituting "1" in order, "{From "}Processing up to(又は「doFrom "endProcessing up to)To execute. The value increases by 1 each time it is repeated.(「|variable|"Part is optional)。
object.times do |variable|
Process to be executed 1
Process to be executed 2
end
length means "length". In Ruby, length is a method for checking "string length" and "array length (number of array elements)".
We will explain how to use the lenght method when checking the "string length" and how to use the lenght method when checking the "array length".
How to use the lenght method to find out the "string length"
str = '123456789'
str.length
# => 9
How to use the lenght method when checking the "array length"
data = [1,2,3,4,5,6,7,8,9]
data.length
# => 9
I think there are various other methods. I will update it every day. Also, I would appreciate it if you could point out any mistakes.