Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c7a37c10
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,发现更多精彩内容 >>
提交
c7a37c10
编写于
3月 01, 2015
作者:
T
Todd Bealmear
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add Enumerable#without
上级
e1e2b54e
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
46 addition
and
0 deletion
+46
-0
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+5
-0
activesupport/lib/active_support/core_ext/array/grouping.rb
activesupport/lib/active_support/core_ext/array/grouping.rb
+12
-0
activesupport/lib/active_support/core_ext/enumerable.rb
activesupport/lib/active_support/core_ext/enumerable.rb
+11
-0
activesupport/test/core_ext/enumerable_test.rb
activesupport/test/core_ext/enumerable_test.rb
+7
-0
guides/source/active_support_core_extensions.md
guides/source/active_support_core_extensions.md
+11
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
c7a37c10
*
Added
`#without`
on
`Enumerable`
and
`Array`
to return a copy of an
enumerable without the specified elements.
*Todd Bealmear*
*
Fixed a problem where String#truncate_words would get stuck with a complex
string.
...
...
activesupport/lib/active_support/core_ext/array/grouping.rb
浏览文件 @
c7a37c10
...
...
@@ -113,4 +113,16 @@ def split(value = nil)
results
end
end
# Returns a copy of the Array without the specified elements.
#
# people = ["David", "Rafael", "Aaron", "Todd"]
# people.without "Aaron", "Todd"
# => ["David", "Rafael"]
#
# Note: This is an optimization of `Enumerable#without` that uses `Array#-`
# instead of `Array#reject` for performance reasons.
def
without
(
*
elements
)
self
-
elements
end
end
activesupport/lib/active_support/core_ext/enumerable.rb
浏览文件 @
c7a37c10
...
...
@@ -60,6 +60,17 @@ def many?
def
exclude?
(
object
)
!
include
?(
object
)
end
# Returns a copy of the enumerable without the specified elements.
#
# ["David", "Rafael", "Aaron", "Todd"].without "Aaron", "Todd"
# => ["David", "Rafael"]
#
# {foo: 1, bar: 2, baz: 3}.without :bar
# => {foo: 1, baz: 3}
def
without
(
*
elements
)
reject
{
|
element
|
element
.
in?
(
elements
)
}
end
end
class
Range
#:nodoc:
...
...
activesupport/test/core_ext/enumerable_test.rb
浏览文件 @
c7a37c10
...
...
@@ -103,4 +103,11 @@ def test_exclude?
assert_equal
true
,
GenericEnumerable
.
new
([
1
]).
exclude?
(
2
)
assert_equal
false
,
GenericEnumerable
.
new
([
1
]).
exclude?
(
1
)
end
def
test_without
assert_equal
[
1
,
2
,
4
],
GenericEnumerable
.
new
((
1
..
5
).
to_a
).
without
(
3
,
5
)
assert_equal
[
1
,
2
,
4
],
(
1
..
5
).
to_a
.
without
(
3
,
5
)
assert_equal
[
1
,
2
,
4
],
(
1
..
5
).
to_set
.
without
(
3
,
5
)
assert_equal
({
foo:
1
,
baz:
3
},
{
foo:
1
,
bar:
2
,
baz:
3
}.
without
(
:bar
))
end
end
guides/source/active_support_core_extensions.md
浏览文件 @
c7a37c10
...
...
@@ -2182,6 +2182,17 @@ to_visit << node if visited.exclude?(node)
NOTE: Defined in
`active_support/core_ext/enumerable.rb`
.
### `without`
The method
`without`
returns a copy of an enumerable with the specified elements
removed:
```
ruby
people
.
without
(
"Aaron"
,
"Todd"
)
```
NOTE: Defined in
`active_support/core_ext/enumerable.rb`
.
Extensions to `Array`
---------------------
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录