D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Enumerable
/
Filename :
sum-i.ri
back
Copy
U:RDoc::AnyMethod[iI"sum:ETI"Enumerable#sum;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"With no block given, ;TI"9returns the sum of +initial_value+ and the elements:;To:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [I"%(1..100).sum # => 5050 ;TI"%(1..100).sum(1) # => 5051 ;TI"*('a'..'d').sum('foo') # => "fooabcd" ;T:@format0o; ; [I"IGenerally, the sum is computed using methods <tt>+</tt> and +each+; ;TI"Cfor performance optimizations, those methods may not be used, ;TI"Gand so any redefinition of those methods may not have effect here.;T@o; ; [I"LOne such optimization: When possible, computes using Gauss's summation ;TI"formula <em>n(n+1)/2</em>:;T@o;; [I"#100 * (100 + 1) / 2 # => 5050 ;T; 0o; ; [I"<With a block given, calls the block with each element; ;TI"Dreturns the sum of +initial_value+ and the block return values:;T@o;; [ I":(1..4).sum {|i| i*i } # => 30 ;TI";(1..4).sum(100) {|i| i*i } # => 130 ;TI".h = {a: 0, b: 1, c: 2, d: 3, e: 4, f: 5} ;TI"9h.sum {|key, value| value.odd? ? value : 0 } # => 9 ;TI"=('a'..'f').sum('x') {|c| c < 'd' ? c : '' } # => "xabc";T; 0: @fileI"enum.c;T:0@omit_headings_from_table_of_contents_below0I"isum(initial_value = 0) -> number sum(initial_value = 0) {|element| ... } -> object ;T0[ I"(*args);T@,FI"Enumerable;TcRDoc::NormalModule00