array = [1,2,3,{:name => "Peter"}, "hello"]
array.each do |element| # it can be "inject", "map" or other iterators
# How to return object "array" and position of "element"
# also next and priviouse "element"
end
当然我可以通过array.index[element]
返回索引,但我在寻找更自然的解决方案。就像Rails的协会proxy_owner
迭代时返回迭代对象和索引
的Ruby 1.8.7
我要输出什么?我想返回对象,我迭代(在我的情况下数组),迭代次数(each_with_index的情况下索引)和迭代的priviouse元素。
作为输入我有一个阵列和迭代器(每个,地图,注入等)
不'each_with_index'工作?如果是这样,你可以做'array [i-1]','array [i + 1]'。 – sawa 2011-03-26 19:58:37
'inject_with_index'或'select_with_index'怎么办? :)我认为那里提出了共同的方法 – fl00r 2011-03-26 20:00:47
什么是期望的输出?显示实际的输入/输出比试图用文字解释更好。 – tokland 2011-03-26 22:02:35