Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source
Show
Ignore:
Timestamp:
04/05/08 03:52:58 (5 months ago)
Author:
pratik
Message:

Improve documentation.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/core_ext/array/access.rb

    r8224 r9226  
    44      # Makes it easier to access parts of an array. 
    55      module Access 
    6         # Returns the remaining of the array from the +position+. 
     6        # Returns the tail of the array from +position+. 
    77        # 
    8         # Examples:  
    98        #   %w( a b c d ).from(0)  # => %w( a b c d ) 
    109        #   %w( a b c d ).from(2)  # => %w( c d ) 
     
    1413        end 
    1514         
    16         # Returns the beginning of the array up to the +position+. 
     15        # Returns the beginning of the array up to +position+. 
    1716        # 
    18         # Examples:  
    1917        #   %w( a b c d ).to(0)  # => %w( a ) 
    2018        #   %w( a b c d ).to(2)  # => %w( a b c ) 
  • trunk/activesupport/lib/active_support/core_ext/array/conversions.rb

    r9093 r9226  
    3131        end 
    3232 
    33         # Converts an array into a string suitable for use as a URL query string, using the given <tt>key</tt> as the 
    34         # param name. 
    35         # 
    36         # Example: 
     33        # Converts an array into a string suitable for use as a URL query string, 
     34        # using the given +key+ as the param name. 
    3735        # 
    3836        #   ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding" 
     
    8179        # 
    8280        # Root children have as node name the one of the root singularized. 
    83         # 
    84         # Example: 
    8581        # 
    8682        #   [{:foo => 1, :bar => 2}, {:baz => 3}].to_xml 
  • trunk/activesupport/lib/active_support/core_ext/array/extract_options.rb

    r7217 r9226  
    33    module Array #:nodoc: 
    44      module ExtractOptions 
    5         # Extract options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash. 
     5        # Extracts options from a set of arguments. Removes and returns the last 
     6        # element in the array if it's a hash, otherwise returns a blank hash. 
    67        # 
    78        #   def options(*args) 
  • trunk/activesupport/lib/active_support/core_ext/array/grouping.rb

    r9093 r9226  
    55    module Array #:nodoc: 
    66      module Grouping 
    7         # Iterate over an array in groups of a certain size, padding any remaining  
    8         # slots with specified value (<tt>nil</tt> by default) unless it is 
    9         # <tt>false</tt>. 
    10         #  
    11         # Examples: 
     7        # Iterates over the array in groups of size +number+, padding any remaining  
     8        # slots with +fill_with+ unless it is +false+. 
    129        #  
    1310        #   %w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g} 
     
    4340        end 
    4441 
    45         # Divide the array into one or more subarrays based on a delimiting +value+ 
     42        # Divides the array into one or more subarrays based on a delimiting +value+ 
    4643        # or the result of an optional block. 
    47         # 
    48         # Examples: 
    4944        # 
    5045        #   [1, 2, 3, 4, 5].split(3)                # => [[1, 2], [4, 5]] 
  • trunk/activesupport/lib/active_support/core_ext/array/random_access.rb

    r7490 r9226  
    33    module Array #:nodoc: 
    44      module RandomAccess 
    5         # Return a random element from the array. 
     5        # Returns a random element from the array. 
    66        def rand 
    77          self[Kernel.rand(length)] 
  • trunk/activesupport/lib/active_support/core_ext/blank.rb

    r9093 r9226  
    11class Object 
    22  # An object is blank if it's false, empty, or a whitespace string. 
    3   # For example, "", "   ", nil, [], and {} are blank. 
     3  # For example, "", "   ", +nil+, [], and {} are blank. 
    44  # 
    55  # This simplifies 
     6  # 
    67  #   if !address.nil? && !address.empty? 
     8  # 
    79  # to 
     10  # 
    811  #   if !address.blank? 
    912  def blank? 
  • trunk/activesupport/lib/active_support/core_ext/class/removal.rb

    r9093 r9226  
    11class Class #:nodoc: 
    22   
    3   # Will unassociate the class with its subclasses as well as uninitializing the subclasses themselves. 
    4   # >> Integer.remove_subclasses 
    5   # => [Bignum, Fixnum] 
    6   # >> Fixnum 
    7   # NameError: uninitialized constant Fixnum 
     3  # Unassociates the class with its subclasses and removes the subclasses 
     4  # themselves. 
     5  # 
     6  #   Integer.remove_subclasses # => [Bignum, Fixnum] 
     7  #   Fixnum                    # => NameError: uninitialized constant Fixnum 
    88  def remove_subclasses 
    99    Object.remove_subclasses_of(self) 
    1010  end 
    1111 
    12   # Returns a list of classes that inherit from this class in an array. 
    13   # Example: Integer.subclasses => ["Bignum", "Fixnum"] 
     12  # Returns an array with the names of the subclasses of +self+ as strings. 
     13  # 
     14  #   Integer.subclasses # => ["Bignum", "Fixnum"] 
    1415  def subclasses 
    1516    Object.subclasses_of(self).map { |o| o.to_s } 
    1617  end 
    1718 
    18   # Allows you to remove individual subclasses or a selection of subclasses from a class without removing all of them. 
     19  # Removes the classes in +klasses+ from their parent module. 
     20  # 
     21  # Ordinary classes belong to some module via a constant. This method computes 
     22  # that constant name from the class name and removes it from the module it 
     23  # belongs to. 
     24  # 
     25  #   Object.remove_class(Integer) # => [Integer] 
     26  #   Integer                      # => NameError: uninitialized constant Integer 
     27  # 
     28  # Take into account that in general the class object could be still stored 
     29  # somewhere else. 
     30  # 
     31  #   i = Integer                  # => Integer 
     32  #   Object.remove_class(Integer) # => [Integer] 
     33  #   Integer                      # => NameError: uninitialized constant Integer 
     34  #   i.subclasses                 # => ["Bignum", "Fixnum"] 
     35  #   Fixnum.superclass            # => Integer 
    1936  def remove_class(*klasses) 
    2037    klasses.flatten.each do |klass| 
  • trunk/activesupport/lib/active_support/core_ext/date_time/conversions.rb

    r9161 r9226  
    4646        end 
    4747 
    48         # Returns the utc_offset as an +HH:MM formatted string. Examples: 
     48        # Returns the +utc_offset+ as an +HH:MM formatted string. Examples: 
    4949        # 
    5050        #   datetime = DateTime.civil(2000, 1, 1, 0, 0, 0, Rational(-6, 24)) 
  • trunk/activesupport/lib/active_support/core_ext/date/calculations.rb

    r9093 r9226  
    1717 
    1818        module ClassMethods 
    19           # Finds yesterday's date, in the format similar to: Mon, 17 Mar 2008 
     19          # Returns a new Date representing the date 1 day ago (i.e. yesterday's date). 
    2020          def yesterday 
    2121            ::Date.today.yesterday 
    2222          end 
    2323           
    24           # Finds tommorrow's date, in the format similar to: Tue, 18 Mar 2008 
     24          # Returns a new Date representing the date 1 day after today (i.e. tomorrow's date). 
    2525          def tomorrow 
    2626            ::Date.today.tomorrow 
  • trunk/activesupport/lib/active_support/core_ext/integer/even_odd.rb

    r9093 r9226  
    44      # For checking if a fixnum is even or odd. 
    55      # 
    6       # Examples: 
    7       # 
    86      #   1.even? # => false 
    97      #   1.odd?  # => true 
    108      #   2.even? # => true 
    11       #   2.odd? # => false 
     9      #   2.odd? # => false 
    1210      module EvenOdd 
    1311        def multiple_of?(number) 
  • trunk/activesupport/lib/active_support/core_ext/integer/inflections.rb

    r9093 r9226  
    77        # Ordinalize turns a number into an ordinal string used to denote the 
    88        # position in an ordered sequence such as 1st, 2nd, 3rd, 4th. 
    9         # 
    10         # Examples: 
    119        # 
    1210        #   1.ordinalize    # => "1st" 
  • trunk/activesupport/lib/active_support/core_ext/object/instance_variables.rb

    r9093 r9226  
    3838  end 
    3939 
    40   # Copies the instance variables of +object+ into self
     40  # Copies the instance variables of +object+ into +self+
    4141  # 
    4242  # Instance variable names in the +exclude+ array are ignored. If +object+ 
  • trunk/activesupport/lib/active_support/core_ext/string/unicode.rb

    r9093 r9226  
    1111          # 
    1212          #   name = 'Claus MÃŒller' 
    13           #   name.reverse #=> "rell??M sualC" 
    14           #   name.length #=> 13 
     13          #   name.reverse #=> "rell??M sualC" 
     14          #   name.length   #=> 13 
    1515          # 
    16           #   name.chars.reverse.to_s #=> "rellÃŒM sualC" 
    17           #   name.chars.length #=> 12 
     16          #   name.chars.reverse.to_s   #=> "rellÃŒM sualC" 
     17          #   name.chars.length         #=> 12 
    1818          #    
    1919          # 
  • trunk/activesupport/lib/active_support/core_ext/time/conversions.rb

    r8699 r9226  
    2121        end 
    2222 
    23         # Convert to a formatted string. See DATE_FORMATS for builtin formats. 
     23        # Converts to a formatted string. See DATE_FORMATS for builtin formats. 
    2424        # 
    2525        # This method is aliased to <tt>to_s</tt>. 
    2626        # 
    27         # ==== Examples: 
    2827        #   time = Time.now                     # => Thu Jan 18 06:10:17 CST 2007 
    2928        # 
     
    3736        #   time.to_formatted_s(:rfc822)        # => "Thu, 18 Jan 2007 06:10:17 -0600" 
    3837        # 
    39         # == Adding your own time formats to to_formatted_s 
     38        # == Adding your own time formats to +to_formatted_s+ 
    4039        # You can add your own formats to the Time::DATE_FORMATS hash. 
    4140        # Use the format name as the hash key and either a strftime string 
     
    5049        end 
    5150         
    52         # Returns the utc_offset as an +HH:MM formatted string. Examples: 
     51        # Returns the UTC offset as an +HH:MM formatted string. 
    5352        # 
    5453        #   Time.local(2000).formatted_offset         # => "-06:00" 
     
    5857        end 
    5958 
    60         # Convert a Time object to a Date, dropping hour, minute, and second precision. 
     59        # Converts a Time object to a Date, dropping hour, minute, and second precision. 
    6160        # 
    62         # ==== Examples 
    63         #   my_time = Time.now 
    64         #   # => Mon Nov 12 22:59:51 -0500 2007 
     61        #   my_time = Time.now  # => Mon Nov 12 22:59:51 -0500 2007 
     62        #   my_time.to_date     #=> Mon, 12 Nov 2007 
    6563        # 
    66         #   my_time.to_date 
    67         #   #=> Mon, 12 Nov 2007 
    68         # 
    69         #   your_time = Time.parse("1/13/2009 1:13:03 P.M.") 
    70         #   # => Tue Jan 13 13:13:03 -0500 2009 
    71         # 
    72         #   your_time.to_date 
    73         #   # => Tue, 13 Jan 2009 
     64        #   your_time = Time.parse("1/13/2009 1:13:03 P.M.")  # => Tue Jan 13 13:13:03 -0500 2009 
     65        #   your_time.to_date                                 # => Tue, 13 Jan 2009 
    7466        def to_date 
    7567          ::Date.new(year, month, day) 
     
    8476        # Converts a Time instance to a Ruby DateTime instance, preserving UTC offset. 
    8577        # 
    86         # ==== Examples 
    87         #   my_time = Time.now 
    88         #   # => Mon Nov 12 23:04:21 -0500 2007 
     78        #   my_time = Time.now    # => Mon Nov 12 23:04:21 -0500 2007 
     79        #   my_time.to_datetime   # => Mon, 12 Nov 2007 23:04:21 -0500 
    8980        # 
    90         #   my_time.to_datetime 
    91         #   # => Mon, 12 Nov 2007 23:04:21 -0500 
    92         # 
    93         #   your_time = Time.parse("1/13/2009 1:13:03 P.M.") 
    94         #   # => Tue Jan 13 13:13:03 -0500 2009 
    95         # 
    96         #   your_time.to_datetime 
    97         #   # => Tue, 13 Jan 2009 13:13:03 -0500 
     81        #   your_time = Time.parse("1/13/2009 1:13:03 P.M.")  # => Tue Jan 13 13:13:03 -0500 2009 
     82        #   your_time.to_datetime                             # => Tue, 13 Jan 2009 13:13:03 -0500 
    9883        def to_datetime 
    9984          ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400)) 
  • trunk/activesupport/lib/active_support/core_ext/time/zones.rb

    r9107 r9226  
    1818          # Sets a global default time zone, separate from the system time zone in ENV['TZ'].  
    1919          # Accepts either a Rails TimeZone object, a string that identifies a  
    20           # Rails TimeZone object (e.g., "Central Time (US & Canada)"), or a TZInfo::Timezone object 
     20          # Rails TimeZone object (e.g., "Central Time (US & Canada)"), or a TZInfo::Timezone object. 
    2121          # 
    22           # Any Time or DateTime object can use this default time zone, via #in_time_zone. 
    23           # Example: 
     22          # Any Time or DateTime object can use this default time zone, via <tt>in_time_zone</tt>. 
    2423          # 
    2524          #   Time.zone = 'Hawaii'          # => 'Hawaii' 
  • trunk/activesupport/lib/active_support/whiny_nil.rb

    r9093 r9226  
    1 # Extensions to nil which allow for more helpful error messages for 
    2 # people who are new to rails. 
     1# Extensions to +nil+ which allow for more helpful error messages for people who 
     2# are new to Rails. 
    33# 
    4 # The aim is to ensure that when users pass nil to methods where that isn't 
    5 # appropriate, instead of NoMethodError and the name of some method used 
    6 # by the framework users will see a message explaining what type of object 
    7 # was expected. 
    8  
     4# Ruby raises NoMethodError if you invoke a method on an object that does not 
     5# respond to it: 
     6
     7#   $ ruby -e nil.destroy 
     8#   -e:1: undefined method `destroy' for nil:NilClass (NoMethodError) 
     9
     10# With these extensions, if the method belongs to the public interface of the 
     11# classes in NilClass::WHINERS the error message suggests which could be the 
     12# actual intended class: 
     13
     14#   $ script/runner nil.destroy  
     15#   ... 
     16#   You might have expected an instance of ActiveRecord::Base. 
     17#   ... 
     18
     19# NilClass#id exists in Ruby 1.8 (though it is deprecated). Since +id+ is a fundamental 
     20# method of Active Record models NilClass#id is redefined as well to raise a RuntimeError 
     21# and warn the user. She probably wanted a model database identifier and the 4 
     22# returned by the original method could result in obscure bugs. 
     23
     24# The flag <tt>config.whiny_nils</tt> determines whether this feature is enabled. 
     25# By default it is on in development and test modes, and it is off in production 
     26# mode. 
    927class NilClass 
    1028  WHINERS = [::Array] 
     
    1937  end 
    2038 
    21   # Raises a RuntimeError when you attempt to call id on nil or a nil object
     39  # Raises a RuntimeError when you attempt to call +id+ on +nil+
    2240  def id 
    2341    raise RuntimeError, "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id", caller 
     
    2947    end 
    3048 
    31     # Raises a NoMethodError when you attempt to call a method on nil, or a nil object
     49    # Raises a NoMethodError when you attempt to call a method on +nil+
    3250    def raise_nil_warning_for(class_name = nil, selector = nil, with_caller = nil) 
    3351      message = "You have a nil object when you didn't expect it!"