5.初探数组

初探数组

知识点

  • 数组定义
  • 数组循环
  • 数组连接

数组定义

定义一个数组

1
2
games = ["绝地逃生", "怪物猎人世界", "信长之野望大志"]
puts games

数组循环

循环数组的内容

1
2
3
4
5
6
7
8
#方法1
games.each do |game|
puts "我爱<<#{game}>>"
end
#方法2
games.each_with_index do |game,i|
puts "#{i}.#{game}"
end

数组连接

把数组的内容连接起来,一起输出

1
puts games.join(",")

判断是否是一个数组

1
2
3
if games.respond_to?("each") #games.respond_to?("each_with_index")
...
end