Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e68b97a4
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,发现更多精彩内容 >>
提交
e68b97a4
编写于
10月 12, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #7007 from Mik-die/hash_extract
make Hash#extract! more symmetric with Hash#slice
上级
3e75369b
a4b11961
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
49 addition
and
6 deletion
+49
-6
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+11
-0
activesupport/lib/active_support/core_ext/hash/slice.rb
activesupport/lib/active_support/core_ext/hash/slice.rb
+3
-3
activesupport/test/core_ext/hash_ext_test.rb
activesupport/test/core_ext/hash_ext_test.rb
+25
-1
guides/source/active_support_core_extensions.md
guides/source/active_support_core_extensions.md
+10
-2
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
e68b97a4
## Rails 4.0.0 (unreleased) ##
*
Hash#extract! returns only those keys that present in the receiver.
{:a => 1, :b => 2}.extract!(:a, :x) # => {:a => 1}
*Mikhail Dieterle*
*
Hash#extract! returns the same subclass, that the receiver is. I.e.
HashWithIndifferentAccess#extract! returns HashWithIndifferentAccess instance.
*Mikhail Dieterle*
*
Optimize ActiveSupport::Cache::Entry to reduce memory and processing overhead.
*Brian Durand*
*
Tests tag the Rails log with the current test class and test case:
...
...
activesupport/lib/active_support/core_ext/hash/slice.rb
浏览文件 @
e68b97a4
...
...
@@ -32,9 +32,9 @@ def slice!(*keys)
# Removes and returns the key/value pairs matching the given keys.
#
# { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b)
#
# => {:a => 1, :b => 2
}
# { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b)
# => { a: 1, b: 2 }
#
{ a: 1, b: 2 }.extract!(:a, :x) # => { a: 1
}
def
extract!
(
*
keys
)
keys
.
each_with_object
(
{})
{
|
key
,
result
|
result
[
key
]
=
delete
(
key
)
}
keys
.
each_with_object
(
self
.
class
.
new
)
{
|
key
,
result
|
result
[
key
]
=
delete
(
key
)
if
has_key?
(
key
)
}
end
end
activesupport/test/core_ext/hash_ext_test.rb
浏览文件 @
e68b97a4
...
...
@@ -723,8 +723,32 @@ def test_indifferent_slice_access_with_symbols
def
test_extract
original
=
{
:a
=>
1
,
:b
=>
2
,
:c
=>
3
,
:d
=>
4
}
expected
=
{
:a
=>
1
,
:b
=>
2
}
remaining
=
{
:c
=>
3
,
:d
=>
4
}
assert_equal
expected
,
original
.
extract!
(
:a
,
:b
)
assert_equal
expected
,
original
.
extract!
(
:a
,
:b
,
:x
)
assert_equal
remaining
,
original
end
def
test_extract_nils
original
=
{
:a
=>
nil
,
:b
=>
nil
}
expected
=
{
:a
=>
nil
}
extracted
=
original
.
extract!
(
:a
,
:x
)
assert_equal
expected
,
extracted
assert_equal
nil
,
extracted
[
:a
]
assert_equal
nil
,
extracted
[
:x
]
end
def
test_indifferent_extract
original
=
{
:a
=>
1
,
'b'
=>
2
,
:c
=>
3
,
'd'
=>
4
}.
with_indifferent_access
expected
=
{
:a
=>
1
,
:b
=>
2
}.
with_indifferent_access
remaining
=
{
:c
=>
3
,
:d
=>
4
}.
with_indifferent_access
[[
'a'
,
'b'
],
[
:a
,
:b
]].
each
do
|
keys
|
copy
=
original
.
dup
assert_equal
expected
,
copy
.
extract!
(
*
keys
)
assert_equal
remaining
,
copy
end
end
def
test_except
...
...
guides/source/active_support_core_extensions.md
浏览文件 @
e68b97a4
...
...
@@ -2867,8 +2867,16 @@ The method `extract!` removes and returns the key/value pairs matching the given
```
ruby
hash
=
{
:a
=>
1
,
:b
=>
2
}
rest
=
hash
.
extract!
(
:a
)
# => {:a => 1}
hash
# => {:b => 2}
rest
=
hash
.
extract!
(
:a
,
:x
)
# => {:a => 1} # non-existing keys are ignored
hash
# => {:b => 2}
```
The method
`extract!`
returns the same subclass of Hash, that the receiver is.
```
ruby
hash
=
{
:a
=>
1
,
:b
=>
2
}.
with_indifferent_access
rest
=
hash
.
extract!
(
:a
).
class
# => ActiveSupport::HashWithIndifferentAccess
```
NOTE: Defined in
`active_support/core_ext/hash/slice.rb`
.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录