簡體   English   中英

使用Proc with ruby​​ hash merge結果

[英]Using Proc with ruby hash merge results in

我試圖通過使用Procs來干掉我的代碼。 我有很多行看起來像這樣(其他行可能有費用,折扣,稅收,而不是收入):

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

我試着寫一個看起來像這樣的Proc:

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

通過這樣做簡化第一行:

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

但是,我收到錯誤:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM