Returns the object in enum that gives the maximum value from the given block.
If no block is given, an enumerator is returned instead.
a = %w(albatross dog horse) a.max_by { |x| x.length } #=> "albatross"
If the n
argument is given, maximum n
elements are returned as an array. These n
elements are sorted by the value from the given block, in descending order.
a = %w[albatross dog horse] a.max_by(2) {|x| x.length } #=> ["albatross", "horse"]
enum.max_by(n) can be used to implement weighted random sampling. Following example implements and use Enumerable#wsample.
module Enumerable # weighted random sampling. # # Pavlos S. Efraimidis, Paul G. Spirakis # Weighted random sampling with a reservoir # Information Processing Letters # Volume 97, Issue 5 (16 March 2006) def wsample(n) self.max_by(n) {|v| rand ** (1.0/yield(v)) } end end e = (-20..20).to_a*10000 a = e.wsample(20000) {|x| Math.exp(-(x/5.0)**2) # normal distribution } # a is 20000 samples from e. p a.length #=> 20000 h = a.group_by {|x| x } -10.upto(10) {|x| puts "*" * (h[x].length/30.0).to_i if h[x] } #=> * # *** # ****** # *********** # ****************** # ***************************** # ***************************************** # **************************************************** # *************************************************************** # ******************************************************************** # *********************************************************************** # *********************************************************************** # ************************************************************** # **************************************************** # *************************************** # *************************** # ****************** # *********** # ******* # *** # *
Returns the maximum value in the range. Returns nil
if the begin value of the range larger than the end value.
Can be given an optional block to override the default comparison method a <=> b
.
(10..20).max #=> 20
Returns the object in enum with the maximum value. The first form assumes all objects implement Comparable
; the second uses the block to return a <=> b.
a = %w(albatross dog horse) a.max #=> "horse" a.max { |a, b| a.length <=> b.length } #=> "albatross"
If the n
argument is given, maximum n
elements are returned as an array, sorted in descending order.
a = %w[albatross dog horse] a.max(2) #=> ["horse", "dog"] a.max(2) {|a, b| a.length <=> b.length } #=> ["albatross", "horse"] [5, 1, 3, 4, 2].max(3) #=> [5, 4, 3]
Returns the maximum size of the queue.
Returns the max number of flags interned to symbols.
Sets the max number of flags interned to symbols.
Returns a two element array containing the objects in enum that correspond to the minimum and maximum values respectively from the given block.
If no block is given, an enumerator is returned instead.
a = %w(albatross dog horse) a.minmax_by { |x| x.length } #=> ["dog", "albatross"]
Sorts enum using a set of keys generated by mapping the values in enum through the given block.
If no block is given, an enumerator is returned instead.
%w{apple pear fig}.sort_by { |word| word.length} #=> ["fig", "pear", "apple"]
The current implementation of sort_by
generates an array of tuples containing the original collection element and the mapped value. This makes sort_by
fairly expensive when the keysets are simple.
require 'benchmark' a = (1..100000).map { rand(100000) } Benchmark.bm(10) do |b| b.report("Sort") { a.sort } b.report("Sort by") { a.sort_by { |a| a } } end
produces:
user system total real Sort 0.180000 0.000000 0.180000 ( 0.175469) Sort by 1.980000 0.040000 2.020000 ( 2.013586)
However, consider the case where comparing the keys is a non-trivial operation. The following code sorts some files on modification time using the basic sort
method.
files = Dir["*"] sorted = files.sort { |a, b| File.new(a).mtime <=> File.new(b).mtime } sorted #=> ["mon", "tues", "wed", "thurs"]
This sort is inefficient: it generates two new File
objects during every comparison. A slightly better technique is to use the Kernel#test
method to generate the modification times directly.
files = Dir["*"] sorted = files.sort { |a, b| test(?M, a) <=> test(?M, b) } sorted #=> ["mon", "tues", "wed", "thurs"]
This still generates many unnecessary Time
objects. A more efficient technique is to cache the sort keys (modification times in this case) before the sort. Perl users often call this approach a Schwartzian Transform, after Randal Schwartz. We construct a temporary array, where each element is an array containing our sort key along with the filename. We sort this array, and then extract the filename from the result.
sorted = Dir["*"].collect { |f| [test(?M, f), f] }.sort.collect { |f| f[1] } sorted #=> ["mon", "tues", "wed", "thurs"]
This is exactly what sort_by
does internally.
sorted = Dir["*"].sort_by { |f| test(?M, f) } sorted #=> ["mon", "tues", "wed", "thurs"]
Groups the collection by result of the block. Returns a hash where the keys are the evaluated result from the block and the values are arrays of elements in the collection that correspond to the key.
If no block is given an enumerator is returned.
(1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
Returns the object in enum that gives the minimum value from the given block.
If no block is given, an enumerator is returned instead.
a = %w(albatross dog horse) a.min_by { |x| x.length } #=> "dog"
If the n
argument is given, minimum n
elements are returned as an array. These n
elements are sorted by the value from the given block.
a = %w[albatross dog horse] p a.min_by(2) {|x| x.length } #=> ["dog", "horse"]
Makes hsh compare its keys by their identity, i.e. it will consider exact same objects as same keys.
h1 = { "a" => 100, "b" => 200, :c => "c" } h1["a"] #=> 100 h1.compare_by_identity h1.compare_by_identity? #=> true h1["a".dup] #=> nil # different objects. h1[:c] #=> "c" # same symbols are all same.
Returns true
if hsh will compare its keys by their identity. Also see Hash#compare_by_identity
.
Fetch the engine as specified by the id
String
OpenSSL::Engine.by_id("openssl") => #<OpenSSL::Engine id="openssl" name="Software engine support">
See OpenSSL::Engine.engines
for the currently loaded engines
Returns a duplicate table object, in column mode. This is handy for chaining in a single call without changing the table mode, but be aware that this method can consume a fair amount of memory for bigger data sets.
This method returns the duplicate table for chaining. Don’t chain destructive methods (like []=()) this way though, since you are working with a duplicate.
Switches the mode of this table to column mode. All calls to indexing and iteration methods will work with columns until the mode is changed again.
This method returns the table and is safe to chain.
Returns a duplicate table object, in row mode. This is handy for chaining in a single call without changing the table mode, but be aware that this method can consume a fair amount of memory for bigger data sets.
This method returns the duplicate table for chaining. Don’t chain destructive methods (like []=()) this way though, since you are working with a duplicate.
Switches the mode of this table to row mode. All calls to indexing and iteration methods will work with rows until the mode is changed again.
This method returns the table and is safe to chain.
Removes the gemspec matching full_name
from the dependency list
Find
the best specification matching a name
and requirements
. Raises if the dependency doesn’t resolve to a valid specification.
Return the best specification that contains the file matching path
.
Returns a duplicate table object, in mixed mode. This is handy for chaining in a single call without changing the table mode, but be aware that this method can consume a fair amount of memory for bigger data sets.
This method returns the duplicate table for chaining. Don’t chain destructive methods (like []=()) this way though, since you are working with a duplicate.
Switches the mode of this table to mixed mode. All calls to indexing and iteration methods will use the default intelligent indexing system until the mode is changed again. In mixed mode an index is assumed to be a row reference while anything else is assumed to be column access by headers.
This method returns the table and is safe to chain.
Remove everything in the DependencyList
that matches but doesn’t satisfy items in dependencies
(a hash of gem names to arrays of dependencies).
Returns every spec that matches name
and optional requirements
.
Return the best specification that contains the file matching path
amongst the specs that are not activated.