Yields each environment variable name and its value as a 2-element Array:
h = {} ENV.each_pair { |name, value| h[name] = value } # => ENV h # => {"bar"=>"1", "foo"=>"0"}
Returns an Enumerator
if no block given:
h = {} e = ENV.each_pair # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_pair> e.each { |name, value| h[name] = value } # => ENV h # => {"bar"=>"1", "foo"=>"0"}
Returns an array with bindir attached to each executable in the executables
list
Redirects to url
with a WEBrick::HTTPStatus::Redirect
status
.
Example:
res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect
Quietly ensure the Gem directory dir
contains all the proper subdirectories for handling default gems. If we can’t create a directory due to a permission problem, then we will silently continue.
If mode
is given, missing directories are created with this mode.
World-writable directories will never be created.
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"]
Array
Difference
Returns a new array that is a copy of the original array, removing all occurrences of any item that also appear in other_ary
. The order is preserved from the original array.
It compares elements using their hash
and eql?
methods for efficiency.
[ 1, 1, 2, 2, 3, 3, 4, 5 ].difference([ 1, 2, 4 ]) #=> [ 3, 3, 5 ]
Note that while 1 and 2 were only present once in the array argument, and were present twice in the receiver array, all occurrences of each Integer
are removed in the returned array.
Multiple array arguments can be supplied and all occurrences of any element in those supplied arrays that match the receiver will be removed from the returned array.
[ 1, 'c', :s, 'yep' ].difference([ 1 ], [ 'a', 'c' ]) #=> [ :s, "yep" ]
If you need set-like behavior, see the library class Set
.
See also Array#-
.
Extracts the nested value specified by the sequence of idx objects by calling dig
at each step, returning nil
if any intermediate step is nil
.
a = [[1, [2, 3]]] a.dig(0, 1, 1) #=> 3 a.dig(1, 2, 3) #=> nil a.dig(0, 0, 0) #=> TypeError: Integer does not have #dig method [42, {foo: :bar}].dig(1, :foo) #=> :bar
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
Prints obj on the given port (default $>
). Equivalent to:
def display(port=$>) port.write self nil end
For example:
1.display "cat".display [ 4, 5, 6 ].display puts
produces:
1cat[4, 5, 6]
Performs integer division: returns the integer result of dividing int
by numeric
.
Returns the floating point result of dividing int
by numeric
.
654321.fdiv(13731) #=> 47.652829364212366 654321.fdiv(13731.24) #=> 47.65199646936475 -654321.fdiv(13731) #=> -47.652829364212366
Returns the digits of int
‘s place-value representation with radix base
(default: 10). The digits are returned as an array with the least significant digit as the first array element.
base
must be greater than or equal to 2.
12345.digits #=> [5, 4, 3, 2, 1] 12345.digits(7) #=> [4, 6, 6, 0, 5] 12345.digits(100) #=> [45, 23, 1] -12345.digits(7) #=> Math::DomainError
Performs division as each part is a float, never returns a float.
Complex(11, 22).fdiv(3) #=> (3.6666666666666665+7.333333333333333i)
Returns float division.
Uses /
to perform division, then converts the result to an integer. Numeric
does not define the /
operator; this is left to subclasses.
Equivalent to num.divmod(numeric)[0]
.
See Numeric#divmod
.
Returns an array containing the quotient and modulus obtained by dividing num
by numeric
.
If q, r = x.divmod(y)
, then
q = floor(x/y) x = q*y + r
The quotient is rounded toward negative infinity, as shown in the following table:
a | b | a.divmod(b) | a/b | a.modulo(b) | a.remainder(b) ------+-----+---------------+---------+-------------+--------------- 13 | 4 | 3, 1 | 3 | 1 | 1 ------+-----+---------------+---------+-------------+--------------- 13 | -4 | -4, -3 | -4 | -3 | 1 ------+-----+---------------+---------+-------------+--------------- -13 | 4 | -4, 3 | -4 | 3 | -1 ------+-----+---------------+---------+-------------+--------------- -13 | -4 | 3, -1 | 3 | -1 | -1 ------+-----+---------------+---------+-------------+--------------- 11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5 ------+-----+---------------+---------+-------------+--------------- 11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5 ------+-----+---------------+---------+-------------+--------------- -11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5 ------+-----+---------------+---------+-------------+--------------- -11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5
Examples
11.divmod(3) #=> [3, 2] 11.divmod(-3) #=> [-4, -1] 11.divmod(3.5) #=> [3, 0.5] (-11).divmod(3.5) #=> [-4, 3.0] 11.5.divmod(3.5) #=> [3, 1.0]
Returns the Encoding
object that represents the encoding of obj.
Returns float / numeric
, same as Float#/
.
Returns the birth time for file.
File.new("testfile").birthtime #=> Wed Apr 09 08:53:14 CDT 2003
If the platform doesn’t have birthtime, raises NotImplementedError
.
Divide by the specified value.
If specified and less than the number of significant digits of the result, the result is rounded to that number of digits, according to BigDecimal.mode
.
If digits is 0, the result is the same as for the / operator or quo
.
If digits is not specified, the result is an integer, by analogy with Float#div
; see also BigDecimal#divmod
.
Examples:
a = BigDecimal("4") b = BigDecimal("3") a.div(b, 3) # => 0.133e1 a.div(b, 0) # => 0.1333333333333333333e1 a / b # => 0.1333333333333333333e1 a.quo(b) # => 0.1333333333333333333e1 a.div(b) # => 1
Divides by the specified value, and returns the quotient and modulus as BigDecimal
numbers. The quotient is rounded towards negative infinity.
For example:
require 'bigdecimal' a = BigDecimal("42") b = BigDecimal("9") q, m = a.divmod(b) c = q * b + m a == c #=> true
The quotient q is (a/b).floor, and the modulus is the amount that must be added to q * b to get a.