LoginSignup
36
42

More than 3 years have passed since last update.

【Ruby】paizaで頻出メソッドまとめてみた

Last updated at Posted at 2020-05-31

参考文献

入力メソッド

//入力値:文字列 改行:あり
input = gets

//入力値:文字列 改行:なし
input = gets.chomp

//入力値:数値   改行:あり
input = gets.to_i

//入力値:数値  改行:なし
input = gets.chomp.to_i

配列メソッド

//入力値を配列に格納 ※数値 
input = gets.split.map(&:to_i)

//分割して配列に格納
a,b,c = gets.split(" ").map &:to_i

//入力値を順番に格納
a = readlines.map &:to_i


//複数行 && 複数要素の格納  ※文字列
lines = []
while line = gets
    lines << line.chomp.split(' ')
end


//複数行 && 複数要素の格納  ※数値
lines = []
while line = gets
    lines << line.chomp.split(' ').map(&:to_i)
end

timesメソッド + 配列

//繰り返し回数
n = 5
//配列の設定
sample = []

//繰り返し回数分、配列に格納
n.times do
  sample.push(gets.chomp.split(" ").map &:to_i)
end

eachメソッド + 配列

//入力値取得
sample = readlines.map &:to_i

//変数宣言
n = 5

//eachメソッド
sample.each do |i|
 if n < 10
    n += 1
 else
    puts "sample"
 end
end

その他

//絶対値取得
n = 5
sample = n.abs


//単数形→複数形
sample = "post".pluralize
puts sample
36
42
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
42