U:RDoc::AnyMethod[iI"max_by:ETI"Enumerable#max_by;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I">Returns the object in enum that gives the maximum ;TI" value from the given block.;To:RDoc::Markup::BlankLine o;
; [I"=If no block is given, an enumerator is returned instead.;T@o:RDoc::Markup::Verbatim; [I"!a = %w(albatross dog horse)
;TI"1a.max_by { |x| x.length } #=> "albatross"
;T:@format0o;
; [I"EIf the +n+ argument is given, maximum +n+ elements are returned ;TI"Fas an array. These +n+ elements are sorted by the value from the ;TI"&given block, in descending order.;T@o;; [I"!a = %w[albatross dog horse]
;TI" ["albatross", "horse"]
;T;
0o;
; [I"Genum.max_by(n) can be used to implement weighted random sampling. ;TI"=Following example implements and use Enumerable#wsample.;T@o;; [-I"module Enumerable
;TI"# # weighted random sampling.
;TI" #
;TI"0 # Pavlos S. Efraimidis, Paul G. Spirakis
;TI"3 # Weighted random sampling with a reservoir
;TI"( # Information Processing Letters
;TI", # Volume 97, Issue 5 (16 March 2006)
;TI" def wsample(n)
;TI"6 self.max_by(n) {|v| rand ** (1.0/yield(v)) }
;TI" end
;TI" end
;TI"e = (-20..20).to_a*10000
;TI"a = e.wsample(20000) {|x|
;TI"3 Math.exp(-(x/5.0)**2) # normal distribution
;TI"}
;TI""# a is 20000 samples from e.
;TI"p a.length #=> 20000
;TI"h = a.group_by {|x| x }
;TI"D-10.upto(10) {|x| puts "*" * (h[x].length/30.0).to_i if h[x] }
;TI"#=> *
;TI"
# ***
;TI"# ******
;TI"# ***********
;TI"# ******************
;TI"'# *****************************
;TI"3# *****************************************
;TI"># ****************************************************
;TI"I# ***************************************************************
;TI"N# ********************************************************************
;TI"Q# ***********************************************************************
;TI"Q# ***********************************************************************
;TI"H# **************************************************************
;TI"># ****************************************************
;TI"1# ***************************************
;TI"%# ***************************
;TI"# ******************
;TI"# ***********
;TI"# *******
;TI"
# ***
;TI"
# *;T;
0:
@fileI"enum.c;T:0@omit_headings_from_table_of_contents_below0I"°enum.max_by {|obj| block } -> obj
enum.max_by -> an_enumerator
enum.max_by(n) {|obj| block } -> obj
enum.max_by(n) -> an_enumerator
;T0[ I"(p1 = v1);T@NFI"Enumerable;TcRDoc::NormalModule00