mirror of
https://github.com/github/rails.git
synced 2026-01-30 16:58:15 -05:00
Fix potential extra space in Array#to_sentence. Closes #10327 [kamal]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8251 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -10,16 +10,17 @@ module ActiveSupport #:nodoc:
|
||||
def to_sentence(options = {})
|
||||
options.assert_valid_keys(:connector, :skip_last_comma)
|
||||
options.reverse_merge! :connector => 'and', :skip_last_comma => false
|
||||
|
||||
options[:connector] = "#{options[:connector]} " unless options[:connector].nil? || options[:connector].strip == ''
|
||||
|
||||
case length
|
||||
when 0
|
||||
""
|
||||
when 0
|
||||
""
|
||||
when 1
|
||||
self[0]
|
||||
when 2
|
||||
"#{self[0]} #{options[:connector]} #{self[1]}"
|
||||
"#{self[0]} #{options[:connector]}#{self[1]}"
|
||||
else
|
||||
"#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]} #{self[-1]}"
|
||||
"#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -47,7 +48,7 @@ module ActiveSupport #:nodoc:
|
||||
to_default_s
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def to_xml(options = {})
|
||||
raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
|
||||
|
||||
|
||||
@@ -38,6 +38,10 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase
|
||||
|
||||
def test_to_sentence_with_connector
|
||||
assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:connector => 'and also')
|
||||
assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
|
||||
assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => nil)
|
||||
assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => ' ')
|
||||
assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
|
||||
end
|
||||
|
||||
def test_to_sentence_with_skip_last_comma
|
||||
@@ -46,11 +50,13 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase
|
||||
|
||||
def test_two_elements
|
||||
assert_equal "one and two", ['one', 'two'].to_sentence
|
||||
assert_equal "one two", ['one', 'two'].to_sentence(:connector => '')
|
||||
end
|
||||
|
||||
def test_one_element
|
||||
assert_equal "one", ['one'].to_sentence
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class ArrayExtToSTests < Test::Unit::TestCase
|
||||
|
||||
Reference in New Issue
Block a user