-[Ruby] I tried to summarize the methods that frequently appear in paiza
-How to receive value from Ruby standard input
//Creating an array for the sample
sample = [1,2,3,4]
//Create an array to store the output results
result = []
//Object: sample array
sample.each do |i|
//Variable t: Stored in variable t from the sample array
if i == 1
//Store in the array created above
result.push(i)
end
//When the input value is one line
input = gets.split.map(&:to_i)
//Store input values in an array at once
input = readlines.map &:to_i
//Variable declaration+Array ①
a,b,c = gets.split(" ").map &:to_i
//Variable declaration+Array ② No line breaks
a,b,c = gets.chomp.split(" ").map &:to_i
//Store input values in an array without line breaks
input = readlines(chomp: true).map(&:to_s)
-** include? Method **: Determine if a particular value exists in the array'' -** blank? Method **:
Determine if the contents of the array exist''
sample = ["a","b","c"]
//include?Method
if.sample.include?("a")
puts "true"
end
//blank?Method
if.sample.blank?
puts "true"
end
//include?Method+variable
sample_num = 3
if.sample.include?("#{sample_num}")
puts "true"
end
//sample array creation
sample = [1,2,3,4,5]
//each_Split with cons method
result = sample.each_cons(2).to_a
//Calculated with each method
result.each do |t|
puts result[1] - result[0]
end
Recommended Posts