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"]
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) #=> NoMethodError, undefined method `dig' for 1:Fixnum [42, {foo: :bar}].dig(1, :foo) #=> :bar
Performs integer division: returns integer value.
See Numeric#divmod
.
Returns the floating point result of dividing big by numeric.
-1234567890987654321.fdiv(13731) #=> -89910996357705.5 -1234567890987654321.fdiv(13731.24) #=> -89909424858035.7
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 -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 the named file.
file_name can be an IO
object.
File.birthtime("testfile") #=> Wed Apr 09 08:53:13 CDT 2003
If the platform doesn’t have birthtime, raises NotImplementedError
.
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
.
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 end
For example:
1.display "cat".display [ 4, 5, 6 ].display puts
produces:
1cat456
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.new("42") b = BigDecimal.new("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.
Performs division and returns the value as a float.
Rational(2, 3).fdiv(1) #=> 0.6666666666666666 Rational(2, 3).fdiv(0.5) #=> 1.3333333333333333 Rational(2).fdiv(3) #=> 0.6666666666666666
Creates a date object denoting the given ordinal date.
The day of year should be a negative or a positive number (as a relative day from the end of year when negative). It should not be zero.
Date.ordinal(2001) #=> #<Date: 2001-01-01 ...> Date.ordinal(2001,34) #=> #<Date: 2001-02-03 ...> Date.ordinal(2001,-1) #=> #<Date: 2001-12-31 ...>
See also jd and new.
Creates a date-time object denoting the given ordinal date.
DateTime.ordinal(2001,34) #=> #<DateTime: 2001-02-03T00:00:00+00:00 ...> DateTime.ordinal(2001,34,4,5,6,'+7') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.ordinal(2001,-332,-20,-55,-54,'+7') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
Extracts the nested value specified by the sequence of key
objects by calling dig
at each step, returning nil
if any intermediate step is nil
.
klass = Struct.new(:a) o = klass.new(klass.new({b: [1, 2, 3]})) o.dig(:a, :a, :b, 0) #=> 1 o.dig(:b, 0) #=> nil
Returns the first object in the range, or an array of the first n
elements.
(10..20).first #=> 10 (10..20).first(3) #=> [10, 11, 12]