LoginSignup
1

More than 5 years have passed since last update.

rubyにおける配列

Posted at

配列とは

一つの変数に複数の値を入れるオブジェクト。順番が自動でつき、その順番を指定することで任意の値を取り出すことができる。

配列の書き方

test

colors = ["red","white"]
colors[1]

左から順に0から数字を振られる。

添字(そえじ)について

test

colors = ["red","white"]
colors[-1]
# 後ろから指定される
colors[1..3]
# 1から3
colors[1...3]
# 1から3の直前
colors[4]
# nilが返される

配列の要素を書き換えたい時

test

colors[1] = "green"

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