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 ...
随机推荐
- codeforces 568a//Primes or Palindromes?// Codeforces Round #315 (Div. 1)
题意:求使pi(n)*q<=rub(n)*p成立的最大的n. 先收集所有的质数和回文数.质数好搜集.回文数奇回文就0-9的数字,然后在头尾添加一个数.在x前后加a,就是x*10+a+a*pow( ...
- Python基础--Python简介和入门
☞写在前面 在说Python之前,我想先说一下自己为什么要学Python,我本人之前也了解过Python,但没有深入学习.之前接触的语言都是Java,也写过一些Java自动化用例,对Java语言只能说 ...
- Tips for Sync Vimtex and PDF
vimtex synctex: \lv. skim synctex: to display the TeX source line corresponding to a point in the PD ...
- 秒杀多线程第三篇 原子操作 Interlocked系列函数
上一篇<多线程第一次亲密接触 CreateThread与_beginthreadex本质区别>中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是 ...
- hdu-1849-nim模板
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 关于vue Unexpected identifier 问题
vue对于es6虽然自带babel转换 但是在index.html文件中并不会发生转换 因此在index.html中使用新的语法会导致低版本浏览器不识别代码因此报出Unexpected identif ...
- VAE demo
先看tflearn 官方的: from __future__ import division, print_function, absolute_import import numpy as np i ...
- powerDesigner16x64安装步骤
PowerDesigner16x64_Evaluation安装 补一下 安装成功
- Openwrt Support MINI-PCIE EC20 (6)
1 Scope of Document This document describes MINI PCIE usb hardware design, for EC20 4G module 2 ...
- 设置xml中控件的圆润边框效果
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...