Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6b2f3774
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6b2f3774
编写于
5月 07, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #12746 from coreyward/master
Fix Hash#deep_merge bug and improve documentation — resolves #12738
上级
73fb39b6
04381344
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
36 addition
and
15 deletion
+36
-15
activesupport/lib/active_support/core_ext/hash/deep_merge.rb
activesupport/lib/active_support/core_ext/hash/deep_merge.rb
+22
-11
activesupport/test/core_ext/hash_ext_test.rb
activesupport/test/core_ext/hash_ext_test.rb
+14
-4
未找到文件。
activesupport/lib/active_support/core_ext/hash/deep_merge.rb
浏览文件 @
6b2f3774
class
Hash
# Returns a new hash with +self+ and +other_hash+ merged recursively.
#
# h1 = {
x: { y: [4, 5, 6] }, z: [7, 8, 9]
}
# h2 = {
x: { y: [7, 8, 9] }, z: 'xyz'
}
# h1 = {
a: true, b: { c: [1, 2, 3] }
}
# h2 = {
a: false, b: { x: [3, 4, 5] }
}
#
# h1.deep_merge(h2) # => {x: {y: [7, 8, 9]}, z: "xyz"}
# h2.deep_merge(h1) # => {x: {y: [4, 5, 6]}, z: [7, 8, 9]}
# h1.deep_merge(h2) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
# # => {:x=>{:y=>[4, 5, 6, 7, 8, 9]}, :z=>[7, 8, 9, "xyz"]}
# h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
#
# Like with Hash#merge in the standard library, a block can be provided
# to merge values:
#
# h1 = { a: 100, b: 200, c: { c1: 100 } }
# h2 = { b: 250, c: { c1: 200 } }
# h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
# # => { a: 100, b: 450, c: { c1: 300 } }
def
deep_merge
(
other_hash
,
&
block
)
dup
.
deep_merge!
(
other_hash
,
&
block
)
end
# Same as +deep_merge+, but modifies +self+.
def
deep_merge!
(
other_hash
,
&
block
)
other_hash
.
each_pair
do
|
k
,
v
|
tv
=
self
[
k
]
if
tv
.
is_a?
(
Hash
)
&&
v
.
is_a?
(
Hash
)
self
[
k
]
=
tv
.
deep_merge
(
v
,
&
block
)
other_hash
.
each_pair
do
|
current_key
,
other_value
|
this_value
=
self
[
current_key
]
self
[
current_key
]
=
if
this_value
.
is_a?
(
Hash
)
&&
other_value
.
is_a?
(
Hash
)
this_value
.
deep_merge
(
other_value
,
&
block
)
else
self
[
k
]
=
block
&&
tv
?
block
.
call
(
k
,
tv
,
v
)
:
v
if
block_given?
&&
key?
(
current_key
)
block
.
call
(
current_key
,
this_value
,
other_value
)
else
other_value
end
end
end
self
end
end
activesupport/test/core_ext/hash_ext_test.rb
浏览文件 @
6b2f3774
...
...
@@ -697,6 +697,16 @@ def test_deep_merge_with_block
assert_equal
expected
,
hash_1
end
def
test_deep_merge_with_falsey_values
hash_1
=
{
e:
false
}
hash_2
=
{
e:
'e'
}
expected
=
{
e:
[
:e
,
false
,
'e'
]
}
assert_equal
(
expected
,
hash_1
.
deep_merge
(
hash_2
)
{
|
k
,
o
,
n
|
[
k
,
o
,
n
]
})
hash_1
.
deep_merge!
(
hash_2
)
{
|
k
,
o
,
n
|
[
k
,
o
,
n
]
}
assert_equal
expected
,
hash_1
end
def
test_deep_merge_on_indifferent_access
hash_1
=
HashWithIndifferentAccess
.
new
({
:a
=>
"a"
,
:b
=>
"b"
,
:c
=>
{
:c1
=>
"c1"
,
:c2
=>
"c2"
,
:c3
=>
{
:d1
=>
"d1"
}
}
})
hash_2
=
HashWithIndifferentAccess
.
new
({
:a
=>
1
,
:c
=>
{
:c1
=>
2
,
:c3
=>
{
:d2
=>
"d2"
}
}
})
...
...
@@ -905,11 +915,11 @@ def test_except_with_mocha_expectation_on_original
def
test_compact
hash_contain_nil_value
=
@symbols
.
merge
(
z:
nil
)
hash_with_only_nil_values
=
{
a:
nil
,
b:
nil
}
h
=
hash_contain_nil_value
.
dup
assert_equal
(
@symbols
,
h
.
compact
)
assert_equal
(
hash_contain_nil_value
,
h
)
h
=
hash_with_only_nil_values
.
dup
assert_equal
({},
h
.
compact
)
assert_equal
(
hash_with_only_nil_values
,
h
)
...
...
@@ -918,11 +928,11 @@ def test_compact
def
test_compact!
hash_contain_nil_value
=
@symbols
.
merge
(
z:
nil
)
hash_with_only_nil_values
=
{
a:
nil
,
b:
nil
}
h
=
hash_contain_nil_value
.
dup
assert_equal
(
@symbols
,
h
.
compact!
)
assert_equal
(
@symbols
,
h
)
h
=
hash_with_only_nil_values
.
dup
assert_equal
({},
h
.
compact!
)
assert_equal
({},
h
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录