From fe4abaa7df98aa736485b6ac863d05d80d2c1d89 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 11 Sep 2005 06:14:05 +0000 Subject: Added Array#to_sentence that'll turn ['one', 'two', 'three'] into 'one, two, and three' #2157 [m.stienstra@fngtps.com] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2185 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../active_support/core_ext/array/conversions.rb | 22 ++++++++++++++++++++++ .../lib/active_support/core_ext/array/to_param.rb | 12 ------------ 2 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/array/conversions.rb delete mode 100644 activesupport/lib/active_support/core_ext/array/to_param.rb (limited to 'activesupport/lib/active_support/core_ext/array') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb new file mode 100644 index 0000000000..19220da2b1 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -0,0 +1,22 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Array #:nodoc: + # Enables to conversion of Arrays to human readable lists. ['one', 'two', 'three'] => "one, two, and three" + module Conversions + # Converts the array to comma-seperated sentence where the last element is joined by the connector word (default is 'and'). + def to_sentence(connector = 'and') + if length > 1 + "#{self[0...-1].join(', ')}, #{connector} #{self[-1]}" + elsif length == 1 + self[0] + end + end + + # When an array is given to url_for, it is converted to a slash separated string. + def to_param + join '/' + end + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/array/to_param.rb b/activesupport/lib/active_support/core_ext/array/to_param.rb deleted file mode 100644 index 85e91e6b1a..0000000000 --- a/activesupport/lib/active_support/core_ext/array/to_param.rb +++ /dev/null @@ -1,12 +0,0 @@ -module ActiveSupport #:nodoc: - module CoreExtensions #:nodoc: - module Array #:nodoc: - module ToParam #:nodoc: - # When an array is given to url_for, it is converted to a slash separated string. - def to_param - join '/' - end - end - end - end -end -- cgit v1.2.3