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 48
48:         def to_formatted_s(format = :default)
49:           case format
50:             when :db
51:               if respond_to?(:empty?) && self.empty?
52:                 "null"
53:               else
54:                 collect { |element| element.id }.join(",")
55:               end
56:             else
57:               to_default_s
58:           end
59:         end

Calls to_param on all its elements and joins the result with slashes. This is used by url_for in Action Pack.

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 28
28:         def to_param
29:           map(&:to_param).join '/'
30:         end

Converts an array into a string suitable for use as a URL query string, using the given key as the param name.

Example:

  ['Rails', 'coding'].to_query('hobbies') => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 37
37:         def to_query(key)
38:           collect { |value| value.to_query("#{key}[]") } * '&'
39:         end

Converts the array to a comma-separated 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 10
10:         def to_sentence(options = {})
11:           options.assert_valid_keys(:connector, :skip_last_comma)
12:           options.reverse_merge! :connector => 'and', :skip_last_comma => false
13:           options[:connector] = "#{options[:connector]} " unless options[:connector].nil? || options[:connector].strip == ''
14: 
15:           case length
16:             when 0
17:               ""
18:             when 1
19:               self[0].to_s
20:             when 2
21:               "#{self[0]} #{options[:connector]}#{self[1]}"
22:             else
23:               "#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}"
24:           end
25:         end

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 61
61:         def to_xml(options = {})
62:           raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
63: 
64:           options[:root]     ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
65:           options[:children] ||= options[:root].singularize
66:           options[:indent]   ||= 2
67:           options[:builder]  ||= Builder::XmlMarkup.new(:indent => options[:indent])
68: 
69:           root     = options.delete(:root).to_s
70:           children = options.delete(:children)
71: 
72:           if !options.has_key?(:dasherize) || options[:dasherize]
73:             root = root.dasherize
74:           end
75: 
76:           options[:builder].instruct! unless options.delete(:skip_instruct)
77: 
78:           opts = options.merge({ :root => children })
79: 
80:           xml = options[:builder]
81:           if empty?
82:             xml.tag!(root, options[:skip_types] ? {} : {:type => "array"})
83:           else
84:             xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) {
85:               yield xml if block_given?
86:               each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) }
87:             }
88:           end
89:         end

[Validate]