Module ActiveSupport::CoreExtensions::Array::Conversions
In: vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb

Methods

Public Instance methods

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 34
34:         def to_formatted_s(format = :default)
35:           case format
36:             when :db
37:               if respond_to?(:empty?) && self.empty?
38:                 "null"
39:               else
40:                 collect { |element| element.id }.join(",")
41:               end
42:             else
43:               to_default_s
44:           end
45:         end

When an array is given to url_for, it is converted to a slash separated string.

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 25
25:         def to_param
26:           join '/'
27:         end

Converts the array to comma-seperated sentence where the last element is joined by the connector word. Options:

  • :connector: The word used to join the last element in arrays with two or more elements (default: "and")
  • :skip_last_comma: Set to true to return "a, b and c" instead of "a, b, and c".

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 8
 8:         def to_sentence(options = {})
 9:           options.assert_valid_keys(:connector, :skip_last_comma)
10:           options.reverse_merge! :connector => 'and', :skip_last_comma => false
11:           
12:           case length
13:                 when 0
14:                         ""
15:             when 1
16:               self[0]
17:             when 2
18:               "#{self[0]} #{options[:connector]} #{self[1]}"
19:             else
20:               "#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]} #{self[-1]}"
21:           end
22:         end

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 47
47:         def to_xml(options = {})
48:           raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
49: 
50:           options[:root]     ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
51:           options[:children] ||= options[:root].singularize
52:           options[:indent]   ||= 2
53:           options[:builder]  ||= Builder::XmlMarkup.new(:indent => options[:indent])
54: 
55:           root     = options.delete(:root).to_s
56:           children = options.delete(:children)
57: 
58:           if !options.has_key?(:dasherize) || options[:dasherize]
59:             root = root.dasherize
60:           end
61: 
62:           options[:builder].instruct! unless options.delete(:skip_instruct)
63: 
64:           opts = options.merge({ :root => children })
65: 
66:           options[:builder].tag!(root) { each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) } }
67:         end

[Validate]