我有钱的日期,我需要在一组日期之间存入新的存款。
所以,让我说哈希是
{"000000001" => "0.00", "000000002" ="12.34", "000000010" => "5.95"}
我希望插入000000008, 54.34
,然后我的结果哈希应该是
{"000000001" => "0.00", "000000002" ="66.68", "000000010" => "5.95"}
*(这些是示例时间戳)
所以...我在想像......
my_hash.each_with_index do |key_value, index|
if index == my_hash.length then return my_hash end
if time >= my_hash[dat[0]][0].to_i and time <= my_hash[dat[0]].next[0].to_i
my_hash[index][1] += value
end
end
end
我知道这是无效的,但我需要帮助。谢谢!
答案 0 :(得分:5)
使用each_cons
:
{
"000000001" => "0.00", "000000002" => "66.68", "000000010" => "5.95"
}.each_cons(2) do |(prev_key, prev_value), (next_key, next_value)|
p "#{prev_key} preceedes #{next_key}"
end
答案 1 :(得分:0)
facets可能有类似你提议的内容。 您可以获得上一个键,如
previous = nil; a.each_key {| K | p'键是',k','previous'是',previous; previous = k}