GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of lifo/doc-rails
Description: Repository for improving Rails documentation
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/zmalltalker/doc-rails.git
Improved documentation for Enumerable#sum in ActiveSupport
leethal (author)
Fri Apr 11 06:52:22 -0700 2008
commit  95f7411d3b4541bcb4d3b695d8a6582b95fa6832
tree    af573ef74717ae89ba0d1ad131cc212a565843eb
parent  87b9d6fad9f46f5ba0e917ddb961698219e8bcc2
...
23
24
25
26
27
 
 
28
29
 
30
31
 
32
33
 
34
35
 
 
 
36
37
38
 
39
40
 
41
42
43
...
23
24
25
 
 
26
27
28
 
29
30
 
31
32
 
33
34
 
35
36
37
38
 
 
39
40
 
41
42
43
44
0
@@ -23,21 +23,22 @@ module Enumerable
0
 
0
   # Calculates a sum from the elements. Examples:
0
   #
0
- # payments.sum { |p| p.price * p.tax_rate }
0
- # payments.sum(&:price)
0
+ # payments.sum { |p| p.price * p.tax_rate }
0
+ # payments.sum(&:price)
0
   #
0
- # This is instead of
0
+ # The latter is a shortcut for:
0
   #
0
- # payments.inject { |sum, p| sum + p.price }
0
+ # payments.inject { |sum, p| sum + p.price }
0
   #
0
- # Also calculates sums without the use of a block:
0
+ # It can also calculate the sum without the use of a block.
0
   #
0
- # [5, 15, 10].sum # => 30
0
+ # [5, 15, 10].sum # => 30
0
+ # ["foo", "bar"].sum # => "foobar"
0
+ # [[1, 2], [3, 1, 5]].sum => [1, 2, 3, 1, 5]
0
   #
0
- # The default identity (sum of an empty list) is zero.
0
- # However, you can override this default:
0
+ # The default sum of an empty list is zero. You can override this default:
0
   #
0
- # [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
0
+ # [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
0
   #
0
   def sum(identity = 0, &block)
0
     return identity unless size > 0

Comments

    No one has commented yet.