LoginSignup
3
3

More than 3 years have passed since last update.

PAIZAのDランクで使ったメソッド一覧

Last updated at Posted at 2020-08-23

はじめに

大前提として答えを掲載することは禁止となっている為、あくまでも参考程度でお願い致します。配列やハッシュは使わなくてもクリアできる為、詳しい説明は致しません。なお投稿者は駆け出しバリバリの実務未経験で、現在Bランクでありまだまだ知識不足が否めません。詳しいメソッドの説明は省くのでご了承をお願いします。

入力値の取得

入力例1(input)
  10

## 10を数値として取得
input = gets.chomp.to_i
=> 10

## 10を文字列として取得
input = gets.chomp.to_s
=> "10"

## 10を分割して数値として取得
input = gets.chomp.split("").map(&:to_i)
=> [1, 0]

## 10を分割して数値として取得
input = gets.chomp.split("").map(&:to_s)
=> ["1", "0"]


入力例2(input)
  10 20

## 10, 20を数値として取得
input = gets.chomp.split.map(&:to_i)
=> [10, 20]

## 10, 20を文字列として取得
input = gets.chomp.split.map(&:to_s)
=> ["10", "20"]


入力例3(input)
1
2
3

## 1,2,3を数字として取得
input = readlines.map(&:chomp).map(&:to_i)
=> [1, 2, 3]

## 1,2,3を文字列として取得
input = readlines.map(&:chomp)
=> ["1", "2", "3"]

readlinesメソッドは、B,Cランクになると使いづらくなるのであまりおすすめしない。

値の出力

出力例1
input = 1

## 1を数値として出力
puts input
=> 1


出力例2
input = ["1", "2", "3"]

## 1,2,3を数値として出力
puts input.map(&:to_i)
=> [1, 2, 3]

## 1だけ文字列として出力
puts input[0]
=> "1"

## 3だけ数値として出力
puts input[2].to_i
=> 3


出力例3
input1, input2, input3 = ["1", "2", "3"]

## 2だけ文字列として出力
puts input2
=> "2"

## 1だけ数値として出力
puts input1.to_i
=> 1

## "1 2 3"という文字列で出力
puts input1 + " " + input2 + " " + input3
若しくは
puts "#{input1} #{input2} #{input3}"
=> "1 2 3"

数値と文字列を意識しないと思い通りの出力ができないので気をつけましょう。

メソッド

mapメソッド

各要素に対してブロックを評価した結果を新しい配列にして返します。

input = [1, 2, 3]
puts input.map { |n| n * 2 }
=> [2, 4, 6]

input = ["1", "2", "3"]
puts input.map { |n| n.to_i }
若しくは
puts input.map(&:to_i)
=> [1, 2, 3]

joinメソッド

各要素を連結した文字列を返します。

input = ["a", "b", "c"]
puts input.join
=> "abc"

puts input.join(",")
=> "a,b,c"

puts input.join(" or ")
=> "a or b or c"

sliceメソッド

指定された自身の要素を返します。

input = ["a", "b", "c"]
puts input.slice(1)
=> "b"

## 1番目から2つ取得
puts input.slice(1, 2)
=> ["b", "c"]

## 破壊的メソッド
input.slice!(0)
puts input
=> ["b", "c"]

input.slice!(1, 2)
puts input
=> "a"

indexメソッド

文字列のインデックスから右に向かってを検索し、最初に見つかった部分文字列の左端のインデックスを返します。見つからなければ nil を返します。

input = [1, 2, 3, 4, 5]

puts input.index(2)
=> 1

puts input.index("2")
=> nil

input = "111111"
## "1"を文字列3番目から検索
puts input.index("1", 3)
=> 3

upcase, downcaseメソッド

全ての小文字を対応する大文字に置き換えた文字列を返します。(upcaseメソッド)
全ての大文字を対応する小文字に置き換えた文字列を返します。(downcaseメソッド)

input = "abcd 1234 GHJK !!"
puts input.upcase
=> "ABCD 1234 GHJK !!"

input = "abcd 1234 GHJK !!"
puts input.downcase
=> "abcd 1234 ghjk !!"

sub, gsubメソッド

文字列中でマッチした最初の部分を置き換えた文字列を生成して返します。
gsubメソッドはマッチした全ての部分を置き換えた文字列を生成して返します。

input = "aaaabbbb11112222"
puts input.sub("a", "A")
=> "Aaaabbbb11112222"

puts input.gsub("a", "A")
=> "AAAAbbbb11112222"

puts input.gsub("a", "A").gsub("1", "3")
=> "AAAAbbbb33332222"

lengthメソッド

文字列の文字数を返します。

input = "aaaabbbb11112222"
puts input.length
=> 16

input = "aaaa bbbb 1111 2222 !"
puts input.length
=> 21

countメソッド

レシーバの要素数を返します。

input = [1, 2, 3, 4, 4]
puts input.count
=> 5

puts input.count(4)
=> 2

## 各要素で2で割ってあまりが0のみ数える
puts input.count {|x| x % 2 == 0}
=> 3

reverseメソッド

文字列を文字単位で左右逆転した文字列を返します。

input = "paiza Drank 12"
puts input.reverse
=> "21 knarD aziap"

input = ["1", 2, "3", "true"]
puts input.reverse
=> ["true", "3", 2, "1"]

shiftメソッド

配列の先頭の要素を取り除いてそれを返します。引数を指定した場合はその個数だけ取り除き、それを配列で返します。

input = [1, 2, 3, 4, 5]
puts input.shift
=> 1
puts input
=> [2, 3, 4, 5]

input = ["a", "b", "c", "d"]
puts input.shift(3)
=> ["a", "b", "c"]
puts input
=> ["d"]

sumメソッド

要素の合計を返します。

input = [1, 2, 3, 4, 5]
puts input.sum
=> 15

input = ["12", "45"]
p input.sum
=> エラー

max, minメソッド

最大の要素を返します。(maxメソッド)
最小の要素を返します。(minメソッド)

input = [1, 2, 3, 4, 5]
puts input.max
=> 5

input = [1, 2, 3, 4, 5]
puts input.min
=> 1

input = ["abcde", "123", "ABCD"]
puts input.max
=> "abcde"

input = ["abcde", "123", "ABCD"]
puts input.min
=> "123"

odd?, even?メソッド

自身が奇数であれば真を返します。そうでない場合は偽を返します。(odd?メソッド)
自身が偶数であれば真を返します。そうでない場合は偽を返します。(even?メソッド)

puts 5.odd?
=> true

puts 2.odd?
=> false

puts 2.even?
=> true

puts 5.even?
=> false

absメソッド

値の絶対値を返します。

puts 100.abs
=> 100

puts -100.abs
=> 100

roundメソッド

自身ともっとも近い整数もしくは実数を返します。いわゆる四捨五入ですが、偶数丸めではありません。

input = 3.141592
puts input.round
=> 3

## 小数点第3位を四捨五入し値を返す
puts input.round(2)
=> 3.14

## 小数点第4位を四捨五入し値を返す
puts input.round(3)
=> 3.142

ceilメソッド

小数点切り上げで値を返す

input = 3.141592
puts input.ceil
=> 4

## 小数点第3位を切り上げて値を返す
puts input.ceil(2)
=> 3.15

## 小数点第4位を切り上げて値を返す
puts input.ceil(3)
=> 3.142

floorメソッド

小数点切り捨てで値を返す

input = 3.141592
puts input.floor
=> 3

## 小数点第3位を切り捨てて値を返す
puts input.floor(2)
=> 3.14

## 小数点第4位を切り捨てて値を返す
puts input.floor(3)
=> 3.141

終わりに

少しでも役に立てて頂ければ光栄です。間違い等がありましたらコメント等でお願い致します。時間に余裕があったらCランクで使ったメソッド等まとめる予定です!

3
3
3

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
3
3