mtch.values_at([index]*) -> array
Uses each index to access the matching values, returning an array of the corresponding matches.
m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie") m.to_a #=> ["HX1138", "H", "X", "113", "8"] m.values_at(0, 2, -2) #=> ["HX1138", "X", "113"] m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2") m.to_a #=> ["1 + 2", "1", "+", "2"] m.values_at(:a, :b, :op) #=> ["1", "2", "+"]
Load the given PStore
file. If read_only
is true, the unmarshalled Hash
will be returned. If read_only
is false, a 3-tuple will be returned: the unmarshalled Hash
, a checksum of the data, and the size of the data.
Iterates the given block over all prime numbers.
See Prime
#each for more details.
Ensures that names
only includes names for the :rdoc, :clobber_rdoc and :rerdoc. If other names are given an ArgumentError
is raised.
Iterates over all IP addresses for name
.
Iterates over all hostnames for address
.
Iterates over all IP addresses for name
.
Iterates over all hostnames for address
.
Sets the system path (the Shell
instance’s PATH environment variable).
path
should be an array of directory name strings.
Convenience method for Shell::CommandProcessor.alias_command
. Defines an instance method which will execute a command under an alternative name.
Shell.def_system_command('date') Shell.alias_command('date_in_utc', 'date', '-u') Shell.new.date_in_utc # => Sat Jan 25 16:59:57 UTC 2014
Convenience method for Shell::CommandProcessor.unalias_command
Returns the execution stack for the target thread—an array containing backtrace location objects.
See Thread::Backtrace::Location
for more information.
This method behaves similarly to Kernel#caller_locations
except it applies to a specific thread.
Returns the Ruby source filename and line number containing this proc or nil
if this proc was not defined in Ruby (i.e. native).
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native).
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native).
Converts block to a Proc
object (and therefore binds it at the point of call) and registers it for execution when the program exits. If multiple handlers are registered, they are executed in reverse order of registration.
def do_at_exit(str1) at_exit { print str1 } end at_exit { puts "cruel world" } do_at_exit("goodbye ") exit
produces:
goodbye cruel world
Ruby tries to load the library named string relative to the requiring file’s path. If the file’s path cannot be determined a LoadError
is raised. If a file is loaded true
is returned and false otherwise.
Returns the current execution stack—an array containing backtrace location objects.
See Thread::Backtrace::Location
for more information.
The optional start parameter determines the number of initial stack entries to omit from the top of the stack.
A second optional length
parameter can be used to limit how many entries are returned from the stack.
Returns nil
if start is greater than the size of current execution stack.
Optionally you can pass a range, which will return an array containing the entries within the specified range.
Returns a new array with the concatenated results of running block once for every element in enum.
If no block is given, an enumerator is returned instead.
[1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4] [[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
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] } #=> * # *** # ****** # *********** # ****************** # ***************************** # ***************************************** # **************************************************** # *************************************************************** # ******************************************************************** # *********************************************************************** # *********************************************************************** # ************************************************************** # **************************************************** # *************************************** # *************************** # ****************** # *********** # ******* # *** # *
Builds a temporary array and traverses that array in reverse order.
If no block is given, an enumerator is returned instead.
(1..3).reverse_each { |v| p v } produces: 3 2 1