参考:http://stackoverflow.com/questions/16159370/ruby-hash-default-value-behavior

使用ruby hash 默认值为空数组,向key 对应的value 追加值然后去get一个不存在的key 时候发现value为 一个非空的arry,不是默认值[]

具体使用示例如下:

 One default Array with mutation

 hsh = Hash.new([])

 hsh[:one] << 'one'
hsh[:two] << 'two' hsh[:nonexistent]
# => ['one', 'two']
# Because we mutated the default value, nonexistent keys return the changed value hsh
# => {}
# But we never mutated the hash itself, therefore it is still empty!
One default Array without mutation hsh = Hash.new([]) hsh[:one] += ['one']
hsh[:two] += ['two']
# This is syntactic sugar for hsh[:two] = hsh[:two] + ['two'] hsh[:nonexistant]
# => []
# We didn't mutate the default value, it is still an empty array hsh
# => { :one => ['one'], :two => ['two'] }
# This time, we *did* mutate the hash.
A new, different Array every time with mutation hsh = Hash.new { [] }
# This time, instead of a default *value*, we use a default *block* hsh[:one] << 'one'
hsh[:two] << 'two' hsh[:nonexistent]
# => []
# We *did* mutate the default value, but it was a fresh one every time. hsh
# => {}
# But we never mutated the hash itself, therefore it is still empty! 47 hsh = Hash.new {|hsh, key| hsh[key] = [] }
# This time, instead of a default *value*, we use a default *block*
# And the block not only *returns* the default value, it also *assigns* it hsh[:one] << 'one'
hsh[:two] << 'two' hsh[:nonexistent]
# => []
# We *did* mutate the default value, but it was a fresh one every time. hsh
# => { :one => ['one'], :two => ['two'], :nonexistent => [] }

ruby hash 默认值的问题的更多相关文章

  1. Ruby方法参数默认值的一个小技巧在Rails中的应用

    我们需要生成一个gravatar格式的html.image标示,于是写了如下方法: def gravatar_for(user) gravatar_id = Digest::MD5::hexdiges ...

  2. Python函数参数默认值的陷阱和原理深究"

    本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...

  3. Ruby Hash与ActiveSupport’s HashWithIndifferentAccess对于key的区别

    Ruby Hash的key定义的时候是支持symbol或者string的,所以访问的时候只能是symbol或者string其中一种方式. 建议使用symbol定义Hash的key,因为symbol在R ...

  4. MySQL 5.6比较重要的参数,以及5.5到5.6默认值有过变化的参数

    新参数说明和设置,这里说下5.6比较重要的参数,以及5.5到5.6默认值有过变化的参数. MySQL Server参数: 1,optimizer_switch:优化器选项. Variable_name ...

  5. Spring boot Jpa添加对象字段使用数据库默认值

    Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...

  6. Ruby  Hash类

    Hash类 更新:2017/06/15 获取没有的哈希值时返回nil 更新:2018/01/03 增加merge! 更新: 2018/04/05 增加搜索 key 更新: 2018/04/30 增加e ...

  7. MySQL设置字段的默认值为当前系统时间

    问题产生: 当我们在对某个字段进行设置时间默认值,该默认值必须是的当前记录的插入时间,那么就将当前系统时间作为该记录创建的时间. 应用场景: 1.在数据表中,要记录每条数据是什么时候创建的,应该由数据 ...

  8. BPM配置故事之案例2-文本默认值

    Boss感觉方便了很多,然而采购部采购员阿海却还是有点意见,他跑来找小明. 阿海:现在申请都是我在提交,申请人和申请部门能不能不要每次都要填写啊,好麻烦的. 小明:没问题,这个简单. 小明在表单中把申 ...

  9. Entity Framework 6 Recipes 2nd Edition(12-7)译 -> 设定默认值

    12-7. 设定默认值 问题 在把一个实体保存到数据库之前,设置该实体属性的默认值 解决方案 假设你有一个如Figure 12-9所示的表, 它保存采购订单(purchase order). 主键Pu ...

随机推荐

  1. wpf窗口阴影

    https://www.cnblogs.com/yiyan127/p/6362509.html

  2. English trip -- Review Unit1 Personal Information 个人信息

    1.重点内容进行自我介绍 What's you name? I'm Loki Where are you from? I'm Local, I'm Chengdu How old are you? t ...

  3. 20170728xlVBA改转置一例

    Sub 导出() Dim Sht As Worksheet, ShtName As String Dim NextRow As Long, NextRow2 As Long Dim iRow As L ...

  4. ns-3

    二.NS-3C++脚本的编写如前所述,NS-3的脚本使用C++语言(也支持python),使用四种类型的网络构件(Node.NetDevice.Channel.Application).一个简单的脚本 ...

  5. Linux系统中切换用户身份su与sudo的用法与实例

    日常操作中为了避免一些误操作,更加安全地管理系统,通常使用的用户身份都为普通用户,而非root.当需要执行一些管理员命令操作时,再切换成root用户身份去执行. 普通用户切换到root用户的方式有:s ...

  6. 在oaf中集成SpringLoaded实现热部署

    首先声明:其实JRebel和Spring-Loaded就是一个开发环境下的利器,skip build and redeploy process,大大提升了工作效率!而非生产环境的利器... 不要在生产 ...

  7. struts2整合axis2后,访问不到wsdl,被struts2拦截的解决办法

    在struts2中整合axis2后,访问wsdl的时候显示404  not found There is no Action mapped for action name xxxxxxxx. 解决办法 ...

  8. Instantclient安装

    Instantclient安装 成功

  9. 安装淘宝cnpm镜像

    $ npm install -g cnpm --registry=https://registry.npm.taobao.org

  10. 6.pragma pack

    下面两个结构体 struct One { double d; char c; int i; } struct Two { char c; double d; int i; } 在#pragma pac ...