Returns an angle with another vector. Result is within the [0..Math::PI].
Vector[1,0].angle_with(Vector[0,1]) # => Math::PI / 2
Creates an OptionParser::Switch
from the parameters. The parsed argument value is passed to the given block, where it can be processed.
See at the beginning of OptionParser
for some full examples.
opts
can include the following elements:
One of the following:
:NONE, :REQUIRED, :OPTIONAL
Acceptable option argument format, must be pre-defined with OptionParser.accept
or OptionParser#accept
, or Regexp
. This can appear once or assigned as String
if not present, otherwise causes an ArgumentError
. Examples:
Float, Time, Array
[:text, :binary, :auto] %w[iso-2022-jp shift_jis euc-jp utf8 binary] { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
Specifies a long style switch which takes a mandatory, optional or no argument. It’s a string of the following form:
"--switch=MANDATORY" or "--switch MANDATORY" "--switch[=OPTIONAL]" "--switch"
Specifies short style switch which takes a mandatory, optional or no argument. It’s a string of the following form:
"-xMANDATORY" "-x[OPTIONAL]" "-x"
There is also a special form which matches character range (not full set of regular expression):
"-[a-z]MANDATORY" "-[a-z][OPTIONAL]" "-[a-z]"
Instead of specifying mandatory or optional arguments directly in the switch parameter, this separate parameter can be used.
"=MANDATORY" "=[OPTIONAL]"
Description string for the option.
"Run verbosely"
If you give multiple description strings, each string will be printed line by line.
Handler for the parsed argument value. Either give a block or pass a Proc
or Method
as an argument.
Add option switch like with on
, but at head of summary.
Add option switch like with on
, but at tail of summary.
Sets the system path (the Shell
instance’s PATH environment variable).
path
should be an array of directory name strings.
Waits until all specified threads have terminated. If a block is provided, it is executed for each thread as they terminate.
Specifies the threads that this object will wait for, but does not actually wait.
Waits until any of the specified threads has terminated, and returns the one that does.
If there is no thread to wait, raises ErrNoWaitingThread
. If nonblock
is true, and there is no terminated thread, raises ErrNoFinishedThread
.
Waits until all of the specified threads are terminated. If a block is supplied for the method, it is executed for each thread termination.
Raises exceptions in the same manner as next_wait
.
Waits until all specified threads have terminated. If a block is provided, it is executed for each thread as they terminate.
Specifies the threads that this object will wait for, but does not actually wait.
Waits until any of the specified threads has terminated, and returns the one that does.
If there is no thread to wait, raises ErrNoWaitingThread
. If nonblock
is true, and there is no terminated thread, raises ErrNoFinishedThread
.
Waits until all of the specified threads are terminated. If a block is supplied for the method, it is executed for each thread termination.
Raises exceptions in the same manner as next_wait
.
Returns the exit value associated with this LocalJumpError
.
Returns an array of the names of the thread-local variables (as Symbols).
thr = Thread.new do Thread.current.thread_variable_set(:cat, 'meow') Thread.current.thread_variable_set("dog", 'woof') end thr.join #=> #<Thread:0x401b3f10 dead> thr.thread_variables #=> [:dog, :cat]
Note that these are not fiber local variables. Please see Thread#[]
and Thread#thread_variable_get
for more details.
Returns true
if the given string (or symbol) exists as a thread-local variable.
me = Thread.current me.thread_variable_set(:oliver, "a") me.thread_variable?(:oliver) #=> true me.thread_variable?(:stanley) #=> false
Note that these are not fiber local variables. Please see Thread#[]
and Thread#thread_variable_get
for more details.
Returns an array of the names of global variables.
global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
Controls tracing of assignments to global variables. The parameter symbol
identifies the variable (as either a string name or a symbol identifier). cmd (which may be a string or a Proc
object) or block is executed whenever the variable is assigned. The block or Proc
object receives the variable’s new value as a parameter. Also see Kernel::untrace_var
.
trace_var :$_, proc {|v| puts "$_ is now '#{v}'" } $_ = "hello" $_ = ' there'
produces:
$_ is now 'hello' $_ is now ' there'
Removes tracing for the specified command on the given global variable and returns nil
. If no command is specified, removes all tracing for that variable and returns an array containing the commands actually removed.
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
Returns the names of the current local variables.
fred = 1 for i in 1..10 # ... end local_variables #=> [:fred, :i]
Sorts enum using a set of keys generated by mapping the values in enum through the given block.
The result is not guaranteed to be stable. When two keys are equal, the order of the corresponding elements is unpredictable.
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"]