Ruby standard input and various methods


This will be a memo for learning.

What is standard input?

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.

gets method

One line input from standard input

line = gets
puts line

to_i method

Read one line from standard input and convert to integer

line =
puts line

//The entered number is output
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:

num1 = gets.to_i
num2 = gets.to_i
for i in num1..num2
    puts i

* To_s method

Convert data to a string

* To_f method

Converting data to a decimal point

chomp method

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

split method

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"]

How to split by specifying the delimiter


str = "samurai,engineer,blog"
array = str.split(",")
p array

[Execution result]

["samurai", "engineer", "blog"]

times method

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

length method

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'
# => 9

How to use the lenght method when checking the "array length"

data = [1,2,3,4,5,6,7,8,9]
# => 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.

Recommended Posts

Ruby standard input and various methods
Ruby standard input
Ruby variables and methods
[Ruby] Singular methods and singular classes
Ruby variables and functions (methods)
Ruby methods and classes (basic)
[Ruby] Singular methods and singular classes
From terminal to Ruby (standard input / output)
Ruby input / output
Ruby standard output
Functions and methods
Ruby Learning # 15 Methods
Ruby and Gem
About Ruby methods
[Java] How to get and output standard input
Ruby: Differences between class methods and instance methods, class variables and instance variables
[ruby] How to receive values from standard input?
[Ruby] Classes and instances
Symbols and Destructive Ruby
[Ruby] Big Decimal and DECIMAL
Ruby Learning # 31 Object Methods
About Ruby instance methods
Ruby classes and instances
Ruby inheritance and delegation
Ruby receives multi-line input
[Ruby] methods, instance methods, etc ...
Various Ruby string operations
Basic methods of Ruby hashes
Coding methods and refactoring principles
Basic methods of Ruby arrays
GraphQL Ruby and actual development
[Java] Generics classes and generics methods
[Ruby] Various types of each
rails path and url methods
Ruby syntax errors and countermeasures
Read standard input in Java
Three Bit Manipulation Methods (Ruby)
About Ruby hashes and symbols
Ruby C extension and volatile
Ruby Learning # 10 Getting User Input
Summarize Ruby and Dependency Injection
Ruby About various iterative processes
About pluck and ids methods
[Ruby] Receive input from console
Java methods and method overloads
What are Ruby class methods?
About Ruby and object model
[Ruby] Class methods, instance methods, etc.
About Ruby classes and instances
Creating Ruby classes and instances
Java abstract methods and classes
How to read a file and treat it as standard input
Learning Ruby with AtCoder 11 How to receive frequently used standard input