Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bad3a120
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,发现更多精彩内容 >>
提交
bad3a120
编写于
7月 17, 2016
作者:
R
Rafael França
提交者:
GitHub
7月 17, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #25811 from oss92/to_sentence_fallback_string
Added :fallback_string option to Array#to_sentence
上级
23843174
b937c24e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
20 addition
and
3 deletion
+20
-3
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+6
-0
activesupport/lib/active_support/core_ext/array/conversions.rb
...esupport/lib/active_support/core_ext/array/conversions.rb
+8
-2
activesupport/test/core_ext/array/conversions_test.rb
activesupport/test/core_ext/array/conversions_test.rb
+6
-1
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
bad3a120
*
Add
`:fallback_string`
option to
`Array#to_sentence`
. If an empty array
calls the function and a fallback string option is set then it returns the
fallback string other than an empty string.
*Mohamed Osama*
*
Fix
`ActiveSupport::TimeZone#strptime`
. Now raises
`ArgumentError`
when the
given time doesn't match the format. The error is the same as the one given
by Ruby's
`Date.strptime`
. Previously it raised
...
...
activesupport/lib/active_support/core_ext/array/conversions.rb
浏览文件 @
bad3a120
...
...
@@ -40,6 +40,12 @@ class Array
# ['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ')
# # => "one or two or at least three"
#
# [].to_sentence(fallback_string: 'none')
# # => "none"
#
# ['one', 'two'].to_sentence(fallback_string: 'none')
# # => "one and two"
#
# Using <tt>:locale</tt> option:
#
# # Given this locale dictionary:
...
...
@@ -57,7 +63,7 @@ class Array
# ['uno', 'dos', 'tres'].to_sentence(locale: :es)
# # => "uno o dos o al menos tres"
def
to_sentence
(
options
=
{})
options
.
assert_valid_keys
(
:words_connector
,
:two_words_connector
,
:last_word_connector
,
:locale
)
options
.
assert_valid_keys
(
:words_connector
,
:two_words_connector
,
:last_word_connector
,
:locale
,
:fallback_string
)
default_connectors
=
{
:words_connector
=>
', '
,
...
...
@@ -72,7 +78,7 @@ def to_sentence(options = {})
case
length
when
0
''
"
#{
options
[
:fallback_string
]
||
''
}
"
when
1
"
#{
self
[
0
]
}
"
when
2
...
...
activesupport/test/core_ext/array/conversions_test.rb
浏览文件 @
bad3a120
...
...
@@ -25,6 +25,11 @@ def test_to_sentence_with_last_word_connector
assert_equal
"one, two and three"
,
[
'one'
,
'two'
,
'three'
].
to_sentence
(
last_word_connector:
' and '
)
end
def
test_to_sentence_with_fallback_string
assert_equal
"none"
,
[].
to_sentence
(
fallback_string:
'none'
)
assert_equal
"one, two, and three"
,
[
'one'
,
'two'
,
'three'
].
to_sentence
(
fallback_string:
'none'
)
end
def
test_two_elements
assert_equal
"one and two"
,
[
'one'
,
'two'
].
to_sentence
assert_equal
"one two"
,
[
'one'
,
'two'
].
to_sentence
(
two_words_connector:
' '
)
...
...
@@ -58,7 +63,7 @@ def test_with_invalid_options
[
'one'
,
'two'
].
to_sentence
(
passing:
'invalid option'
)
end
assert_equal
exception
.
message
,
"Unknown key: :passing. Valid keys are: :words_connector, :two_words_connector, :last_word_connector, :locale"
assert_equal
exception
.
message
,
"Unknown key: :passing. Valid keys are: :words_connector, :two_words_connector, :last_word_connector, :locale
, :fallback_string
"
end
def
test_always_returns_string
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录