我正在遍历一个数组或哈希表,并且我想根据索引获取值。
@lv = {'apple' => ['round', 'red'], 'more names' => ['tags', 'more tags']}
@lv.each do |key, value|
# if the value is from the index key and equal to key itself...
end
我不确定如何从@lv
中获取值。假设我想要获取apple
:
是否有类似于@lv[0]
应该等于apple的东西?因为
[0] => apple
[1] => more names
好的?
那么我可以
@lv.each do |key, value|
if @lv[0] == key
# apple equals apple, then proceed
end
end
这该怎么做的语法是什么?
谢谢!