ruby hash 默认值的问题
参考: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 默认值的问题的更多相关文章
- Ruby方法参数默认值的一个小技巧在Rails中的应用
我们需要生成一个gravatar格式的html.image标示,于是写了如下方法: def gravatar_for(user) gravatar_id = Digest::MD5::hexdiges ...
- Python函数参数默认值的陷阱和原理深究"
本文将介绍使用mutable对象作为Python函数参数默认值潜在的危害,以及其实现原理和设计目的 本博客已经迁移至: http://cenalulu.github.io/ 本篇博文已经迁移,阅读全文 ...
- Ruby Hash与ActiveSupport’s HashWithIndifferentAccess对于key的区别
Ruby Hash的key定义的时候是支持symbol或者string的,所以访问的时候只能是symbol或者string其中一种方式. 建议使用symbol定义Hash的key,因为symbol在R ...
- MySQL 5.6比较重要的参数,以及5.5到5.6默认值有过变化的参数
新参数说明和设置,这里说下5.6比较重要的参数,以及5.5到5.6默认值有过变化的参数. MySQL Server参数: 1,optimizer_switch:优化器选项. Variable_name ...
- Spring boot Jpa添加对象字段使用数据库默认值
Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
- Ruby Hash类
Hash类 更新:2017/06/15 获取没有的哈希值时返回nil 更新:2018/01/03 增加merge! 更新: 2018/04/05 增加搜索 key 更新: 2018/04/30 增加e ...
- MySQL设置字段的默认值为当前系统时间
问题产生: 当我们在对某个字段进行设置时间默认值,该默认值必须是的当前记录的插入时间,那么就将当前系统时间作为该记录创建的时间. 应用场景: 1.在数据表中,要记录每条数据是什么时候创建的,应该由数据 ...
- BPM配置故事之案例2-文本默认值
Boss感觉方便了很多,然而采购部采购员阿海却还是有点意见,他跑来找小明. 阿海:现在申请都是我在提交,申请人和申请部门能不能不要每次都要填写啊,好麻烦的. 小明:没问题,这个简单. 小明在表单中把申 ...
- Entity Framework 6 Recipes 2nd Edition(12-7)译 -> 设定默认值
12-7. 设定默认值 问题 在把一个实体保存到数据库之前,设置该实体属性的默认值 解决方案 假设你有一个如Figure 12-9所示的表, 它保存采购订单(purchase order). 主键Pu ...
随机推荐
- 雷林鹏分享:Ruby 类案例
Ruby 类案例 下面将创建一个名为 Customer 的 Ruby 类,您将声明两个方法: display_details:该方法用于显示客户的详细信息. total_no_of_customers ...
- jquery自动填充输入框
1,这是一个比较简单的页面,你可以复制下来就可以使用.<!doctype html><html lang="en"><head> <met ...
- hdu 1069 DAG加权
题目: Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 无法打开物理文件“xxxx.mdf”。操作系统错误 5:“5(拒绝访问)”
- python 利用quick sort思路实现median函数
# import numpy as np def median(arr): #return np.median(arr) arr.sort() return arr[len(arr)>>1 ...
- linux 日志编程(总结)
转自:http://blog.csdn.net/hemmanhui/article/details/4343844 日志主要涉及3个函数,分别是openlog.syslog和closelog函数.表8 ...
- vs2015 系统找不到指定的文件(异常来自HRESULT:0x80070002)问题的解决方法
vs2015 创建mvc项目时,弹出错误信息内容(系统找不到指定的文件(异常来自HRESULT:0x80070002)) 弹出窗体如下图所示: 导致整个原因是:未安装NuGet包 解决方法: 1)打开 ...
- Wifi Troughput Test using iperf
learning wifi throughput test using iperf [Purpose] Learning how to do wifi throughput test u ...
- Thinking in Java笔记之类及对象的初始化
最近在看<Thinking in Java>这本书,之前一直对类及对象的初始化过程不太清楚,只是感到很模糊.看了这本书关于对象初始化的部分,终于搞明白了. 废话不多说,先上两个例子,实例摘 ...
- DBGRID 拖动滚动条 和 鼠标滚轮的问题
滚动条拖动问题 默认是,拖动时,网格内数据不变,等放开鼠标后才会变. 方法 拖动时同时变,当前记录也变,不用新控件 http://wenwen.sogou.com/z/q185291591.htm 鼠 ...