0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WEB開発をやり始めてみる(Ruby:配列+いろんなメソッド)

Last updated at Posted at 2022-10-16

やったことメモ

配列を使ったメソッドの使い方のまとめ

$ irb
#### 配列の書き方
2.5.0 :001 > a = [1, 2, 3, 'aa', [1, 2, 3]]
 => [1, 2, 3, "aa", [1, 2, 3]]

#### 配列の表示の仕方
2.5.0 :002 > a[0]
 => 1 
2.5.0 :003 > a[1]
 => 2 
2.5.0 :004 > puts a
1
2
3
aa
1
2
3
 => nil 

#### 配列の中身が空かどうか
2.5.0 :005 > a.empty?
 => false 
2.5.0 :006 > b = []
 => [] 
2.5.0 :007 > b.empty?
 => true

#### 配列の中身に〇〇が含まれているか
2.5.0 :008 > a.include?('aa')
 => true 
2.5.0 :009 > a.include?('b')
 => false 

#### 配列の中身を逆順に表示(!を使うと破壊的メソッドといって、元の中身を書き換える)
2.5.0 :010 > a.reverse
 => [[1, 2, 3], "aa", 3, 2, 1] 
2.5.0 :011 > a
 => [1, 2, 3, "aa", [1, 2, 3]] 
2.5.0 :012 > a.reverse!
 => [[1, 2, 3], "aa", 3, 2, 1] 
2.5.0 :013 > a
 => [[1, 2, 3], "aa", 3, 2, 1] 

#### 配列の中身をシャッフルして表示
2.5.0 :014 > a.shuffle
 => [1, 2, "aa", [1, 2, 3], 3] 
2.5.0 :015 > a.shuffle
 => ["aa", 1, 3, [1, 2, 3], 2] 
2.5.0 :016 > a
 => [[1, 2, 3], "aa", 3, 2, 1]

#### 配列を作る(.to_a) (0..25)は0 - 25といった範囲を指定
 2.5.0 :019 > (0..25).to_a
 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] 
2.5.0 :020 > aa = (0..99).to_a.shuffle!
 => [49, 76, 39, 50, 52, 17, 12, 21, 97, 0, 67, 59, 56, 32, 11, 64, 72, 9, 95, 73, 92, 58, 78, 28, 37, 31, 90, 93, 4, 8, 60, 2, 47, 66, 74, 42, 6, 84, 7, 10, 63, 75, 36, 33, 3, 53, 35, 19, 5, 77, 23, 48, 38, 81, 15, 57, 13, 27, 24, 16, 1, 70, 98, 87, 20, 80, 79, 88, 40, 30, 26, 99, 83, 61, 18, 82, 54, 89, 46, 29, 68, 14, 71, 25, 41, 91, 85, 96, 51, 94, 22, 69, 62, 45, 65, 34, 44, 43, 55, 86] 
2.5.0 :021 > aa
 => [49, 76, 39, 50, 52, 17, 12, 21, 97, 0, 67, 59, 56, 32, 11, 64, 72, 9, 95, 73, 92, 58, 78, 28, 37, 31, 90, 93, 4, 8, 60, 2, 47, 66, 74, 42, 6, 84, 7, 10, 63, 75, 36, 33, 3, 53, 35, 19, 5, 77, 23, 48, 38, 81, 15, 57, 13, 27, 24, 16, 1, 70, 98, 87, 20, 80, 79, 88, 40, 30, 26, 99, 83, 61, 18, 82, 54, 89, 46, 29, 68, 14, 71, 25, 41, 91, 85, 96, 51, 94, 22, 69, 62, 45, 65, 34, 44, 43, 55, 86] 
2.5.0 :022 > z = (0..10).to_a
 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

#### 配列の後ろに足す(pushも同様)
2.5.0 :023 > z << 20
 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20] 
2.5.0 :024 > z
 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20] 
2.5.0 :025 > z.push(30)
 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30] 
2.5.0 :027 > z
 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30] 

#### 配列の後ろを削除
2.5.0 :028 > z.pop
 => 30 
2.5.0 :030 > z
 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20] 
2.5.0 :031 > z.shift
 => 0 
2.5.0 :032 > z
 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20] 
2.5.0 :033 > z << 3
 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 3] 
2.5.0 :034 > z << 6
 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 3, 6] 

#### 配列の中身で一緒になっていないものを表示(被っていない)
2.5.0 :035 > z.uniq
 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20] 
2.5.0 :036 > z
 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 3, 6] 
2.5.0 :037 > z.uniq!
 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20] 
2.5.0 :038 > z
 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20] 

#### 配列の中身を結合&どう結合をするか
2.6.3 :007 > s = ["my","name","is","kuruma"]
 => ["my", "name", "is", "kuruma"] 
2.6.3 :008 > s.join
 => "mynameiskuruma" 
2.6.3 :009 > s.join(" ")
 => "my name is kuruma" 
2.6.3 :010 > s.join("_")
 => "my_name_is_kuruma" 

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?