Reduce Hash Values

前端 未结 6 1477
南旧
南旧 2021-02-03 20:30

I am having trouble with the syntax for reduce. I have a hash of the following format:

H = {\"Key1\" => 1, \"Key2\" => 2}

I would like t

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-03 20:57

    You can make elem contain the value by splitting it up in 2 variables:

    H.reduce(0) {|memo, (key, val)| memo += val}
    

提交回复
热议问题