Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10124: as_array_docs.diff

File as_array_docs.diff, 3.7 kB (added by jeremymcanally, 8 months ago)

Doc patch

  • activesupport/lib/active_support/core_ext/array/conversions.rb

    old new  
    44  module CoreExtensions #:nodoc: 
    55    module Array #:nodoc: 
    66      module Conversions 
    7         # Converts the array to comma-seperated sentence where the last element is joined by the connector word. Options: 
     7        # Converts the array to comma-seperated sentence where the last element is joined by the connector word.  
     8        # 
     9        # ==== Options 
    810        # * <tt>:connector</tt> - The word used to join the last element in arrays with two or more elements (default: "and") 
    911        # * <tt>:skip_last_comma</tt> - Set to true to return "a, b and c" instead of "a, b, and c". 
     12        # 
     13        # ==== Examples 
     14        #    ["Lions", "tigers", "bears"].to_sentence 
     15        #    # => "Lions, tigers, and bears" 
     16        # 
     17        #    ["Active Record", "Action Pack", "Active Resource"].to_sentence(:connector => "or") 
     18        #    # => "Active Record, Action Pack, or Active Resource"  
     19        # 
     20        #    ["red", "green", "blue"].to_sentence(:skip_last_comma => true) 
     21        #    # => "red, green and blue" 
     22        # 
    1023        def to_sentence(options = {}) 
    1124          options.assert_valid_keys(:connector, :skip_last_comma) 
    1225          options.reverse_merge! :connector => 'and', :skip_last_comma => false 
     
    2336          end 
    2437        end 
    2538 
    26         # When an array is given to url_for, it is converted to a slash separated string. 
     39        # A method used in +url_for+, which converts an array to a slash separated string. 
     40        # 
     41        # ==== Examples 
     42        #   ["accounts", "new", 3].to_param 
     43        #   # => "accounts/new/3" 
     44        # 
     45        #   ["users", "opa2332", "pages", "personal"].to_param 
     46        #   # => "users/opa2332/pages/personal" 
     47        # 
    2748        def to_param 
    2849          join '/' 
    2950        end 
     
    4869          end 
    4970        end 
    5071         
     72        # Attempts to convert the array and all of its elements to XML.  All elements must respond to 
     73        # to_xml or an error will be raised. 
     74        # 
     75        # ==== Options 
     76        # * <tt>:root</tt> - Sets the name for the root elements in the generated XML 
     77        # * <tt>:children</tt> - Sets the name for the child elements in the generated XML 
     78        # * <tt>:indent</tt> - Sets the how many spaces will be used to indent the generated XML 
     79        # * <tt>:builder</tt> - Allows you to provide your own Builder instance to use 
     80        # 
     81        # ==== Examples 
     82        # 
     83        #   [1,2,3].to_xml 
     84        #   # => RuntimeError: Not all elements respond to to_xml 
     85        #  
     86        #   [{'key' => 'value', 'key2' => 'value'}, {'name' => 'Ruby on Rails'}].to_xml 
     87        #   # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
     88        #         <records type=\"array\"> 
     89        #           <record> 
     90        #             <key2>value</key2> 
     91        #             <key>value</key> 
     92        #           </record> 
     93        #           <record> 
     94        #             <name>Ruby on Rails</name> 
     95        #           </record> 
     96        #         </records>" 
     97        # 
     98        #   [{'key' => 'value'}].to_xml(:root => 'beets', :children => 'carrots') 
     99        #   # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
     100        #         <beets type=\"array\"> 
     101        #           <carrots> 
     102        #             <key>value</key> 
     103        #           </carrots> 
     104        #         </beets>" 
    51105        def to_xml(options = {}) 
    52106          raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } 
    53107