Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a67f25d5
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,发现更多精彩内容 >>
提交
a67f25d5
编写于
1月 09, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #13632 from tinogomes/master
Adding Hash#compact and Hash#compact! methods
上级
4c32b729
51215937
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
63 addition
and
0 deletion
+63
-0
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+4
-0
activesupport/lib/active_support/core_ext/hash.rb
activesupport/lib/active_support/core_ext/hash.rb
+1
-0
activesupport/lib/active_support/core_ext/hash/compact.rb
activesupport/lib/active_support/core_ext/hash/compact.rb
+20
-0
activesupport/test/core_ext/hash_ext_test.rb
activesupport/test/core_ext/hash_ext_test.rb
+28
-0
guides/source/active_support_core_extensions.md
guides/source/active_support_core_extensions.md
+10
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
a67f25d5
*
Added
`Hash#compact`
and
`Hash#compact!`
for removing items with nil value from hash.
*Celestino Gomes*
*
Maintain proleptic gregorian in Time#advance
`Time#advance` uses `Time#to_date` and `Date#advance` to calculate a new date.
...
...
activesupport/lib/active_support/core_ext/hash.rb
浏览文件 @
a67f25d5
require
'active_support/core_ext/hash/compact'
require
'active_support/core_ext/hash/conversions'
require
'active_support/core_ext/hash/deep_merge'
require
'active_support/core_ext/hash/except'
...
...
activesupport/lib/active_support/core_ext/hash/compact.rb
0 → 100644
浏览文件 @
a67f25d5
class
Hash
# Returns a hash with non +nil+ values.
#
# hash = { a: true, b: false, c: nil}
# hash.compact # => { a: true, b: false}
# hash # => { a: true, b: false, c: nil}
# { c: nil }.compact # => {}
def
compact
self
.
select
{
|
_
,
value
|
!
value
.
nil?
}
end
# Replaces current hash with non +nil+ values.
#
# hash = { a: true, b: false, c: nil}
# hash.compact! # => { a: true, b: false}
# hash # => { a: true, b: false}
def
compact!
self
.
reject!
{
|
_
,
value
|
value
.
nil?
}
end
end
activesupport/test/core_ext/hash_ext_test.rb
浏览文件 @
a67f25d5
...
...
@@ -54,6 +54,8 @@ def test_methods
assert_respond_to
h
,
:deep_stringify_keys!
assert_respond_to
h
,
:to_options
assert_respond_to
h
,
:to_options!
assert_respond_to
h
,
:compact
assert_respond_to
h
,
:compact!
end
def
test_transform_keys
...
...
@@ -865,6 +867,32 @@ def test_except_with_mocha_expectation_on_original
original
.
expects
(
:delete
).
never
original
.
except
(
:a
)
end
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
)
end
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
)
end
end
class
IWriteMyOwnXML
...
...
guides/source/active_support_core_extensions.md
浏览文件 @
a67f25d5
...
...
@@ -2907,6 +2907,16 @@ The method `with_indifferent_access` returns an `ActiveSupport::HashWithIndiffer
NOTE: Defined in
`active_support/core_ext/hash/indifferent_access.rb`
.
### Compacting
The methods
`compact`
and
`compact!`
return a Hash without items with
`nil`
value.
```
ruby
{
a:
1
,
b:
2
,
c:
nil
}.
compact
# => {a: 1, b: 2}
```
NOTE: Defined in
`active_support/core_ext/hash/compact.rb`
.
Extensions to `Regexp`
----------------------
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录