参考: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. 利用 AttachThreadInput 改变其它进程的输入法状态

    利用 AttachThreadInput 和 WM_INPUTLANGCHANGEREQUEST 消息 改变 其它 进程 的 输入 状态 ? 众所周知,通过 ActivateKeyboardLayou ...

  2. nyoj737石子合并(一)

    先得出区间为1和2时的结果.用arr[i][j]记录i,j内的和.dp[i][j]记录i,j区间全加起来的最小花费.那么区间大小为1和2时都是明显的.为3时枚举断点.其中一个区间大小为1也是可行的. ...

  3. CodeForces 558B

    Description Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make ...

  4. 通过一个uri获取一个Bitmap对象

    Android 开发过程中,可能会用到的,通过一个uri获取一个Bitmap对象 private Bitmap getBitmapFromUri(Uri uri){  try  {   // 读取ur ...

  5. C# 字符串 相关操作

    你或许知道你能使用String.Trim方法去除字符串的头和尾的空格,不幸运的是. 这个Trim方法不能去除字符串中间的C#空格. static void Main()         {       ...

  6. 有名管道mkfifo

    int mkfifo(const char *pathname, mode_t mode); int mknod(const char *pathname, mode_t mode, dev_t de ...

  7. vs2012 怎样解决 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包的问题

    今天用vs2012打开项目总是报这个错误,在网上找到解决方法 1.问题描述: 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPac ...

  8. 073——VUE中vuex之使用actions和axios异步初始购物车数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Flask初级(六)flash模板渲染

    Project name :Flask_Plan templates:templates static:static 继续上篇的模板 我们已经可以静态调用模板,包括继承模板,保证了页面的一致性,但是我 ...

  10. Shiro 学习资料

    参考链接:http://jinnianshilongnian.iteye.com/blog/2018398