Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f1bad130
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,发现更多精彩内容 >>
提交
f1bad130
编写于
7月 12, 2014
作者:
E
Erik Michaels-Ober
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Return an Enumerator if no block is given
上级
e167389e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
49 addition
and
1 deletion
+49
-1
activesupport/lib/active_support/core_ext/hash/keys.rb
activesupport/lib/active_support/core_ext/hash/keys.rb
+3
-1
activesupport/lib/active_support/core_ext/hash/transform_values.rb
...port/lib/active_support/core_ext/hash/transform_values.rb
+2
-0
activesupport/test/core_ext/hash/transform_keys_test.rb
activesupport/test/core_ext/hash/transform_keys_test.rb
+32
-0
activesupport/test/core_ext/hash/transform_values_test.rb
activesupport/test/core_ext/hash/transform_values_test.rb
+12
-0
未找到文件。
activesupport/lib/active_support/core_ext/hash/keys.rb
浏览文件 @
f1bad130
...
...
@@ -6,7 +6,8 @@ class Hash
# hash.transform_keys{ |key| key.to_s.upcase }
# # => {"NAME"=>"Rob", "AGE"=>"28"}
def
transform_keys
result
=
{}
return
enum_for
(
:transform_keys
)
unless
block_given?
result
=
self
.
class
.
new
each_key
do
|
key
|
result
[
yield
(
key
)]
=
self
[
key
]
end
...
...
@@ -16,6 +17,7 @@ def transform_keys
# Destructively convert all keys using the block operations.
# Same as transform_keys but modifies +self+.
def
transform_keys!
return
enum_for
(
:transform_keys!
)
unless
block_given?
keys
.
each
do
|
key
|
self
[
yield
(
key
)]
=
delete
(
key
)
end
...
...
activesupport/lib/active_support/core_ext/hash/transform_values.rb
浏览文件 @
f1bad130
...
...
@@ -5,6 +5,7 @@ class Hash
# { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 }
# # => { a: 2, b: 4, c: 6 }
def
transform_values
return
enum_for
(
:transform_values
)
unless
block_given?
result
=
self
.
class
.
new
each
do
|
key
,
value
|
result
[
key
]
=
yield
(
value
)
...
...
@@ -14,6 +15,7 @@ def transform_values
# Destructive +transform_values+
def
transform_values!
return
enum_for
(
:transform_values!
)
unless
block_given?
each
do
|
key
,
value
|
self
[
key
]
=
yield
(
value
)
end
...
...
activesupport/test/core_ext/hash/transform_keys_test.rb
0 → 100644
浏览文件 @
f1bad130
require
'abstract_unit'
require
'active_support/core_ext/hash/transform_keys'
class
TransformKeysTest
<
ActiveSupport
::
TestCase
test
"transform_keys returns a new hash with the keys computed from the block"
do
original
=
{
a:
'a'
,
b:
'b'
}
mapped
=
original
.
transform_keys
{
|
k
|
"
#{
k
}
!"
.
to_sym
}
assert_equal
({
a:
'a'
,
b:
'b'
},
original
)
assert_equal
({
a!:
'a'
,
b!:
'b'
},
mapped
)
end
test
"transform_keys! modifies the keys of the original"
do
original
=
{
a:
'a'
,
b:
'b'
}
mapped
=
original
.
transform_keys!
{
|
k
|
"
#{
k
}
!"
.
to_sym
}
assert_equal
({
a!:
'a'
,
b!:
'b'
},
original
)
assert_same
original
,
mapped
end
test
"transform_keys returns an Enumerator if no block is given"
do
original
=
{
a:
'a'
,
b:
'b'
}
enumerator
=
original
.
transform_keys
assert_equal
Enumerator
,
enumerator
.
class
end
test
"transform_keys is chainable with Enumerable methods"
do
original
=
{
a:
'a'
,
b:
'b'
}
mapped
=
original
.
transform_keys
.
with_index
{
|
k
,
i
|
[
k
,
i
].
join
.
to_sym
}
assert_equal
({
a0:
'a'
,
b1:
'b'
},
mapped
)
end
end
activesupport/test/core_ext/hash/transform_values_test.rb
浏览文件 @
f1bad130
...
...
@@ -46,4 +46,16 @@ class TransformValuesTest < ActiveSupport::TestCase
assert_equal
'a!'
,
mapped
[
:a
]
assert_nil
mapped
[
:b
]
end
test
"transform_values returns an Enumerator if no block is given"
do
original
=
{
a:
'a'
,
b:
'b'
}
enumerator
=
original
.
transform_values
assert_equal
Enumerator
,
enumerator
.
class
end
test
"transform_values is chainable with Enumerable methods"
do
original
=
{
a:
'a'
,
b:
'b'
}
mapped
=
original
.
transform_values
.
with_index
{
|
v
,
i
|
[
v
,
i
].
join
}
assert_equal
({
a:
'a0'
,
b:
'b1'
},
mapped
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录