Reduce Hash Values

前端 未结 6 1481
南旧
南旧 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 21:02

    Try this:

    H.reduce(0) { |memo, elem| memo += elem[1] }
    

    or

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

提交回复
热议问题