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