aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authoross92 <mohamed.o.alnagdy@gmail.com>2016-07-13 13:46:43 +0200
committeross92 <mohamed.o.alnagdy@gmail.com>2016-07-13 20:19:26 +0200
commitb937c24edc21ba1b2cb0d6e64d2ccddcad2a7a12 (patch)
treec3d04b80b2317df5155c08749910c2d713a48772 /activesupport/lib/active_support
parente1915e712d654838e4a734a6f9f0327494095c8e (diff)
downloadrails-b937c24edc21ba1b2cb0d6e64d2ccddcad2a7a12.tar.gz
rails-b937c24edc21ba1b2cb0d6e64d2ccddcad2a7a12.tar.bz2
rails-b937c24edc21ba1b2cb0d6e64d2ccddcad2a7a12.zip
Added :fallback_string option to Array#to_sentence
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/core_ext/array/conversions.rb10
1 files changed, 8 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb
index 8718b7e1e5..54fb83581a 100644
--- a/activesupport/lib/active_support/core_ext/array/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -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 @@ class Array
case length
when 0
- ''
+ "#{options[:fallback_string] || ''}"
when 1
"#{self[0]}"
when 2