Ensures the root of chain
has a trusted certificate in trust_dir
and the digests of the two certificates match according to digester
Add a certificate to trusted certificate list.
Returns the first element, or the first n
elements, of the array. If the array is empty, the first form returns nil
, and the second form returns an empty array. See also Array#last
for the opposite effect.
a = [ "q", "r", "s", "t" ] a.first #=> "q" a.first(2) #=> ["q", "r"]
Returns the last element(s) of self
. If the array is empty, the first form returns nil
.
See also Array#first
for the opposite effect.
a = [ "w", "x", "y", "z" ] a.last #=> "z" a.last(2) #=> ["y", "z"]
Returns the index of the last object in self
==
to obj
.
If a block is given instead of an argument, returns the index of the first object for which the block returns true
, starting from the last object.
Returns nil
if no match is found.
See also Array#index
.
If neither block nor argument is given, an Enumerator
is returned instead.
a = [ "a", "b", "b", "b", "c" ] a.rindex("b") #=> 3 a.rindex("z") #=> nil a.rindex { |x| x == "b" } #=> 3
Converts any arguments to arrays, then merges elements of self
with corresponding elements from each argument.
This generates a sequence of ary.size
n-element arrays, where n is one more than the count of arguments.
If the size of any argument is less than the size of the initial array, nil
values are supplied.
If a block is given, it is invoked for each output array
, otherwise an array of arrays is returned.
a = [ 4, 5, 6 ] b = [ 7, 8, 9 ] [1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] [1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]] a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]
Assumes that self
is an array of arrays and transposes the rows and columns.
a = [[1,2], [3,4], [5,6]] a.transpose #=> [[1, 3, 5], [2, 4, 6]]
If the length of the subarrays don’t match, an IndexError
is raised.
Returns a Digest
subclass by name
in a thread-safe manner even when on-demand loading is involved.
require 'digest' Digest("MD5") # => Digest::MD5 Digest(:SHA256) # => Digest::SHA256 Digest(:Foo) # => LoadError: library not found for class Digest::Foo -- digest/foo
Returns true if self
is a prime number, else returns false.
Returns the smallest number than or equal to int
in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits
is positive, self
for zero, and truncate up for negative.
1.truncate #=> 1 1.truncate(2) #=> 1.0 15.truncate(-1) #=> 10
Returns num
truncated to an Integer
.
Numeric
implements this by converting its value to a Float
and invoking Float#truncate
.
Invokes the given block with the sequence of numbers starting at num
, incremented by step
(defaulted to 1
) on each call.
The loop finishes when the value to be passed to the block is greater than limit
(if step
is positive) or less than limit
(if step
is negative), where limit is defaulted to infinity.
In the recommended keyword argument style, either or both of step
and limit
(default infinity) can be omitted. In the fixed position argument style, zero as a step (i.e. num.step(limit, 0)) is not allowed for historical compatibility reasons.
If all the arguments are integers, the loop operates using an integer counter.
If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed the following expression:
floor(n + n*epsilon)+ 1
Where the n
is the following:
n = (limit - num)/step
Otherwise, the loop starts at num
, uses either the less-than (<) or greater-than (>) operator to compare the counter against limit
, and increments itself using the +
operator.
If no block is given, an Enumerator
is returned instead.
For example:
p 1.step.take(4) p 10.step(by: -1).take(4) 3.step(to: 5) { |i| print i, " " } 1.step(10, 2) { |i| print i, " " } Math::E.step(to: Math::PI, by: 0.2) { |f| print f, " " }
Will produce:
[1, 2, 3, 4] [10, 9, 8, 7] 3 4 5 1 3 5 7 9 2.71828182845905 2.91828182845905 3.11828182845905
Returns the index of the last occurrence of the given substring or pattern (regexp) in str. Returns nil
if not found. If the second parameter is present, it specifies the position in the string to end the search—characters beyond this point will not be considered.
"hello".rindex('e') #=> 1 "hello".rindex('l') #=> 3 "hello".rindex('a') #=> nil "hello".rindex(?e) #=> 1 "hello".rindex(/[aeiou]/, -2) #=> 1
If integer is greater than the length of str, returns a new String
of length integer with str left justified and padded with padstr; otherwise, returns str.
"hello".ljust(4) #=> "hello" "hello".ljust(20) #=> "hello " "hello".ljust(20, '1234') #=> "hello123412341234123"
If integer is greater than the length of str, returns a new String
of length integer with str right justified and padded with padstr; otherwise, returns str.
"hello".rjust(4) #=> "hello" "hello".rjust(20) #=> " hello" "hello".rjust(20, '1234') #=> "123412341234123hello"
Returns a copy of str
with the characters in from_str
replaced by the corresponding characters in to_str
. If to_str
is shorter than from_str
, it is padded with its last character in order to maintain the correspondence.
"hello".tr('el', 'ip') #=> "hippo" "hello".tr('aeiou', '*') #=> "h*ll*" "hello".tr('aeiou', 'AA*') #=> "hAll*"
Both strings may use the c1-c2
notation to denote ranges of characters, and from_str
may start with a ^
, which denotes all characters except those listed.
"hello".tr('a-y', 'b-z') #=> "ifmmp" "hello".tr('^aeiou', '*') #=> "*e**o"
The backslash character \
can be used to escape ^
or -
and is otherwise ignored unless it appears at the end of a range or the end of the from_str
or to_str
:
"hello^world".tr("\\^aeiou", "*") #=> "h*ll**w*rld" "hello-world".tr("a\\-eo", "*") #=> "h*ll**w*rld" "hello\r\nworld".tr("\r", "") #=> "hello\nworld" "hello\r\nworld".tr("\\r", "") #=> "hello\r\nwold" "hello\r\nworld".tr("\\\r", "") #=> "hello\nworld" "X['\\b']".tr("X\\", "") #=> "['b']" "X['\\b']".tr("X-\\]", "") #=> "'b'"
Processes a copy of str as described under String#tr
, then removes duplicate characters in regions that were affected by the translation.
"hello".tr_s('l', 'r') #=> "hero" "hello".tr_s('el', '*') #=> "h*o" "hello".tr_s('el', 'hx') #=> "hhxo"
Translates str in place, using the same rules as String#tr
. Returns str, or nil
if no changes were made.
Truncates float
to a given precision in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits
is more than zero.
Transfer control to another fiber, resuming it from where it last stopped or starting it if it was not resumed before. The calling fiber will be suspended much like in a call to Fiber.yield
. You need to require 'fiber'
before using this method.
The fiber which receives the transfer call is treats it much like a resume call. Arguments passed to transfer are treated like those passed to resume.
You cannot resume a fiber that transferred control to another one. This will cause a double resume error. You need to transfer control back to this fiber before it can yield and resume.
Example:
fiber1 = Fiber.new do puts "In Fiber 1" Fiber.yield end fiber2 = Fiber.new do puts "In Fiber 2" fiber1.transfer puts "Never see this message" end fiber3 = Fiber.new do puts "In Fiber 3" end fiber2.resume fiber3.resume
produces
In fiber 2 In fiber 1 In fiber 3
Returns true
if the named file is a directory, false
otherwise.