Help us understand the problem. What is going on with this article?

# Paizaのスキルチェック問題や開発の際によく使うRubyのメソッドをまとめてみた

More than 1 year has passed since last update.

## slice

```alphabet = "abcde"
alphabet.slice(1..2)
```
```"bc"
```

## gsub

```alphabet = "abcde"
alphabet.gsub(/bc/, "a")
```
```"aade"
```

## sort

sortは昇順と降順どちらに並び替えることが可能。

まずは昇順に並び替えたい場合

```numbers = [5, 3, 2, 9, 1]
numbers.sort
```
```[1, 2, 3, 5, 9]
```

```numbers = [5, 3, 2, 9, 1]
numbers.sort { |a, b| b <=> a }
```
```[9, 5, 3, 2, 1]
```

## reverse

```name = "Taro"
name.reverse
```
```"oraT"
```

```numbers = [5, 3, 2, 9, 1]
numbers.reverse
```
```[1, 9, 2, 3, 5]
```

## pushとunshift

pushは、配列の末尾に指定した引数を追加する

```alphabet = ["a", "b", "c", "d"]
alphabet.push("e") #alphabet << "e"でもOK
```
```["a", "b", "c", "d", "e"]
```

unshiftは、配列の先頭に引数で指定した要素を追加する

```alphabet = ["a", "b", "c", "d"]
alphabet.unshift("0")
```
```["0", "a", "b", "c", "d"]
```

## round

```1.3.round
=> 1
1.5.round
=> 2
```

## chomp

```acquisition = gets #getsはキーボードで入力した値を文字列で取得するメソッド
```
```hello
=> "hello\n"
```
```acquisition.chomp
```
```=> "hello"
```

## uniq

```num = [0, 1, 1, 1, 2, 4]
num.uniq
```
```[0, 1, 2, 4]
```

## map

```numbers = ["0", "1", "1", "1", "2", "4"]
numbers.map { |item| item.to_i }
```
```[0, 1, 1, 1, 2, 4]
```

```numbers = ["0", "1", "1", "1", "2", "4"]
numbers.map(&:to_i)
```
```[0, 1, 1, 1, 2, 4]
```

## select

ブロック内の戻り値が真になったものを新たな配列として返す

```numbers = [0, 1, 2, 3, 4]
numbers.select { |item| item % 2 == 0 }
```
```[0, 2, 4]
```

## join

```numbers = ["0", "1", "1", "1", "2", "4"]
numbers.join
```
```"011124"
```

```numbers = ["0", "1", "1", "1", "2", "4"]
numbers.join(",")
```
```"0,1,1,1,2,4"
```

## upcaseとdowncase

それぞれ大文字と小文字にする

```alphabet = "abcde"
alphabet.upcase
```
```"ABCDE"
```

```alphabet = "ABCDE"
alphabet.downcase
```
```"abcde"
```

## abs

```num = -1
num.abs
```
```1
```

## おわりに

Paizaや開発で使っていたrubyのメソッドを紹介しました。随時更新していきたいと思っています。何かもっと便利でよく使ってるよというメソッドがあれば教えてください！

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away