这发生在 Ruby on Rails 的视图中,其中有另一个部分的哈希值。这个散列有大约 20 个键/值对。
有(在 HAML 中)
- if (some_conditon)
= render :partial => 'some_name', :locals => a_hash.merge({ :extra => true })
- else
-# a lot more processing, including concatenating the partials and return as json
- some_var.each do |item|
- result_html << (render :partial => 'some_name', :locals => a_hash )
-# etc
- response.content_type = "application/json"
= result_html.to_json
所以问题是,应该merge
写成merge!
吗?因为以后不再需要它,如果创建一个新的hash,那么创建这个新的hash(hash中有20个项目)会花费大量的时间。如果进行就地修改,可以使用现有的哈希结构,并在其中添加一项,这样会快很多吗?