Using Proc with ruby hash merge results in

浪尽此生 提交于 2019-12-24 16:20:03

问题


I'm trying to DRY up my code by using Procs. I have many lines that look like this (other lines may have fees, discounts, taxes, rather than revenue):

h.merge!({revenue: 500}){|key, old_val, new_val| old_val + new_val}

I tried to write a Proc that looks like this:

hproc = Proc.new {|key, old_val, new_val| old_val + new_val}

And simplify the first line by doing this:

h.merge!({revenue: 500})(&hproc)

However, I get the error:

syntax error, unexpected '(', expecting end-of-input
h.merge!({revenue:600})(&hproc)
                    ^

回答1:


h.merge!({revenue: 500}, &hproc)


来源:https://stackoverflow.com/questions/34386308/using-proc-with-ruby-hash-merge-results-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!