D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Array
/
Filename :
sum-i.ri
back
Copy
U:RDoc::AnyMethod[iI"sum:ETI"Array#sum;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"PWith no block given, returns the sum of +init+ and all elements of +self+; ;TI"7for array +array+ and value +init+, equivalent to:;To:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [I"sum = init ;TI",array.each {|element| sum += element } ;TI" sum ;T:@format0o; ; [I"QFor example, <tt>[e0, e1, e2].sum</tt> returns <tt>init + e0 + e1 + e2</tt>.;T@o; ; [I"Examples:;T@o;; [ I"-[0, 1, 2, 3].sum # => 6 ;TI"/[0, 1, 2, 3].sum(100) # => 106 ;TI":['abc', 'def', 'ghi'].sum('jkl') # => "jklabcdefghi" ;TI"0[[:foo, :bar], ['foo', 'bar']].sum([2, 3]) ;TI"+# => [2, 3, :foo, :bar, "foo", "bar"] ;T; 0o; ; [I"^The +init+ value and elements need not be numeric, but must all be <tt>+</tt>-compatible:;T@o;; [I"># Raises TypeError: Array can't be coerced into Integer. ;TI"+[[:foo, :bar], ['foo', 'bar']].sum(2) ;T; 0o; ; [I"FWith a block given, calls the block with each element of +self+; ;TI"Tthe block's return value (instead of the element itself) is used as the addend:;T@o;; [I"S['zero', 1, :two].sum('Coerced and concatenated: ') {|element| element.to_s } ;TI"/# => "Coerced and concatenated: zero1two" ;T; 0o; ; [I"Notes:;T@o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [o; ; [I"?Array#join and Array#flatten may be faster than Array#sum ;TI"3for an array of strings or an array of arrays.;To;;0; [o; ; [I"[Array#sum method may not respect method redefinition of "+" methods such as Integer#+.;T: @fileI"array.c;T:0@omit_headings_from_table_of_contents_below0I"Fsum(init = 0) -> object sum(init = 0) {|element| ... } -> object ;T0[ I"(*args);T@AFI" Array;TcRDoc::NormalClass00