Write down the method as a memorandum
Gets the value entered by the user on the keyboard as a string
There is always a line break at the end
Standard input
abcd
input = gets
p input
Output result
"abcd\n"
Remove the trailing newline character
and return a new string
Standard input
abcd
input = gets.chomp
p input
Output result
"abcd"
Split a string into a array of strings
Standard input
hello new world
array = gets.split(" ")
p array
Output result
["hello", "new", "world"]
Gets the multi-line value
entered by the user on the keyboard as an array of strings`
Standard input
hello
new
world
array = readlines.chomp
p array
Output result
["hello", "new", "world"]
Iterate as many times as there are elements in the array
array = ["1", "2", "3"]
new_array = array.map{ |v| v.to_i }
p new_array
Output result
[1, 2, 3]
Convert each element of the array to a string and combine the arguments as delimiters
array = ["hello", "new", "world"]
p array.join(" ")
Output result
"hello new world"
Add an argument as an element to the end of the array
array = ["a", "b", "c"]
p array.push("d")
Output result
["a", "b", "c", "d"]
Recommended Posts