有没有办法从数组中获取索引值?
class Datadump
def ddlist()
@tobethrown = [
sofa = ["red", "blue", "striped"],
toys = ["figures", "robots", "comics"]
]
end
def dddig(chkdump)
#check index value of array from string
#I expect [1][2]
end
end
dumpoop = Datadump.new
dumpoop.ddlist()
dumpoop.dddig("comics")
如果竞赛只是["沙发","玩具"]而不是它们作为变量,我可以使用tobethrown.index()获取值。我是红宝石的新手,请原谅我,如果这是一个noob问题。
答案 0 :(得分:0)
@tobethrown.each.with_index do |a, i|
j = a.index(chkdump)
return [i, j] if j
end