1
1

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.

[Ruby]配列のアレコレ

Last updated at Posted at 2022-01-14

基礎的だから難しくはないけど、使ってないと何気忘れそうなので記事に。

配列に値を追加する場合

<<の演算子を使用する

irb(main):001:0> pencil_case = ["ペン", "消しゴム", "定規"]
=> ["ペン", "消しゴム", "定規"]
irb(main):002:0> pencil_case << "えんぴつ"
=> ["ペン", "消しゴム", "定規", "えんぴつ"] #配列に"えんぴつ"が追加される

配列の値を指定して取得する場合

添字を使用する

#配列の要素を取得
配列[添字]

#配列の要素を取得し変数に代入
変数 = 配列[添字]

配列の値を変更する場合

変更したい要素を取り出して、新しい要素を代入する

#配列の要素を変更
配列[添字] =

 
以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?