D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Enumerable
/
Filename :
tally-i.ri
back
Copy
U:RDoc::AnyMethod[iI" tally:ETI"Enumerable#tally;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"(When argument +hash+ is not given, ;TI"Hreturns a new hash whose keys are the distinct elements in +self+; ;TI"Deach integer value is the count of occurrences of each element:;To:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [I"=%w[a b c b c a c b].tally # => {"a"=>2, "b"=>3, "c"=>3} ;T:@format0o; ; [I"$When argument +hash+ is given, ;TI"Jreturns +hash+, possibly augmented; for each element +ele+ in +self+:;T@o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [o; ; [I"KAdds it as a key with a zero value if that key does not already exist:;T@o;; [I"-hash[ele] = 0 unless hash.include?(ele) ;T; 0o;;0; [o; ; [I"'Increments the value of key +ele+:;T@o;; [I"hash[ele] += 1 ;T; 0o; ; [I"IThis is useful for accumulating tallies across multiple enumerables:;T@o;; [ I"&h = {} # => {} ;TI"D%w[a c d b c a].tally(h) # => {"a"=>2, "c"=>2, "d"=>1, "b"=>1} ;TI"L%w[b a z].tally(h) # => {"a"=>3, "c"=>2, "d"=>1, "b"=>2, "z"=>1} ;TI"T%w[b a m].tally(h) # => {"a"=>4, "c"=>2, "d"=>1, "b"=>3, "z"=>1, "m"=>1} ;T; 0o; ; [I"QThe key to be added or found for an element depends on the class of +self+; ;TI"Vsee {Enumerable in Ruby Classes}[rdoc-ref:Enumerable@Enumerable+in+Ruby+Classes].;T@o; ; [I"Examples:;T@o;;;;[o;;0; [o; ; [I"-Array (and certain array-like classes): ;TI"'the key is the element (as above).;To;;0; [o; ; [I"+Hash (and certain hash-like classes): ;TI"Cthe key is the 2-element array formed from the key-value pair:;T@o;; [ I"+h = {} # => {} ;TI"I{foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1} ;TI"i{foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>1, [:bar, "b"]=>1, [:foo, "c"]=>1, [:bar, "d"]=>1} ;TI"i{foo: 'a', bar: 'b'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>1, [:bar, "d"]=>1} ;TI"h{foo: 'c', bar: 'd'}.tally(h) # => {[:foo, "a"]=>2, [:bar, "b"]=>2, [:foo, "c"]=>2, [:bar, "d"]=>2};T; 0: @fileI"enum.c;T:0@omit_headings_from_table_of_contents_below0I"tally(hash = {}) -> hash ;T0[ I"(*args);T@OFI"Enumerable;TcRDoc::NormalModule00