Returns a 2-element array [q, r]
, where
q = (self/other).floor # Quotient r = self % other # Remainder
Of the Core and Standard Library classes, only Rational
uses this implementation.
Examples:
Rational(11, 1).divmod(4) # => [2, (3/1)] Rational(11, 1).divmod(-4) # => [-3, (-1/1)] Rational(-11, 1).divmod(4) # => [-3, (1/1)] Rational(-11, 1).divmod(-4) # => [2, (-3/1)] Rational(12, 1).divmod(4) # => [3, (0/1)] Rational(12, 1).divmod(-4) # => [-3, (0/1)] Rational(-12, 1).divmod(4) # => [-3, (0/1)] Rational(-12, 1).divmod(-4) # => [3, (0/1)] Rational(13, 1).divmod(4.0) # => [3, 1.0] Rational(13, 1).divmod(Rational(4, 11)) # => [35, (3/11)]
Returns the Encoding
object that represents the encoding of obj.
Returns the quotient from dividing self
by other
:
f = 3.14 f.quo(2) # => 1.57 f.quo(-2) # => -1.57 f.quo(Rational(2, 1)) # => 1.57 f.quo(Complex(2, 0)) # => (1.57+0.0i)
Returns a 2-element array [q, r]
, where
q = (self/other).floor # Quotient r = self % other # Remainder
Examples:
11.0.divmod(4) # => [2, 3.0] 11.0.divmod(-4) # => [-3, -1.0] -11.0.divmod(4) # => [-3, 1.0] -11.0.divmod(-4) # => [2, -3.0] 12.0.divmod(4) # => [3, 0.0] 12.0.divmod(-4) # => [-3, 0.0] -12.0.divmod(4) # => [-3, -0.0] -12.0.divmod(-4) # => [3, -0.0] 13.0.divmod(4.0) # => [3, 1.0] 13.0.divmod(Rational(4, 1)) # => [3, 1.0]
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
Writes self
on the given port:
1.display "cat".display [ 4, 5, 6 ].display puts
Output:
1cat[4, 5, 6]
Synonym for $stdin.
Returns a new Date object formed fom the arguments.
With no arguments, returns the date for January 1, -4712:
Date.ordinal.to_s # => "-4712-01-01"
With argument year
, returns the date for January 1 of that year:
Date.ordinal(2001).to_s # => "2001-01-01" Date.ordinal(-2001).to_s # => "-2001-01-01"
With positive argument yday
== n
, returns the date for the nth
day of the given year:
Date.ordinal(2001, 14).to_s # => "2001-01-14"
With negative argument yday
, counts backward from the end of the year:
Date.ordinal(2001, -14).to_s # => "2001-12-18"
Raises an exception if yday
is zero or out of range.
See argument start.
Creates a DateTime
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 ...>
Finds and returns the object in nested objects that is specified by name
and identifiers
. The nested objects may be instances of various classes. See Dig Methods.
Examples:
require "ostruct" address = OpenStruct.new("city" => "Anytown NC", "zip" => 12345) person = OpenStruct.new("name" => "John Smith", "address" => address) person.dig(:address, "zip") # => 12345 person.dig(:business_address, "zip") # => nil
With no argument, returns the first element of self
, if it exists:
(1..4).first # => 1 ('a'..'d').first # => "a"
With non-negative integer argument n
given, returns the first n
elements in an array:
(1..10).first(3) # => [1, 2, 3] (1..10).first(0) # => [] (1..4).first(50) # => [1, 2, 3, 4]
Raises an exception if there is no first element:
(..4).first # Raises RangeError
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
Returns the Encoding
object that represents the encoding of obj.
Returns true if the set and the given enumerable have no element in common. This method is the opposite of intersect?
.
Set[1, 2, 3].disjoint? Set[3, 4] #=> false Set[1, 2, 3].disjoint? Set[4, 5] #=> true Set[1, 2, 3].disjoint? [3, 4] #=> false Set[1, 2, 3].disjoint? 4..5 #=> true
Divides the set into a set of subsets according to the commonality defined by the given block.
If the arity of the block is 2, elements o1 and o2 are in common if block.call(o1, o2) is true. Otherwise, elements o1 and o2 are in common if block.call(o1) == block.call(o2).
require 'set' numbers = Set[1, 3, 4, 6, 9, 10, 11] set = numbers.divide { |i,j| (i - j).abs == 1 } set #=> #<Set: {#<Set: {1}>, # #<Set: {11, 9, 10}>, # #<Set: {3, 4}>, # #<Set: {6}>}>
Returns an enumerator if no block is given.
Finds and returns an object among nested objects. The nested objects may be instances of various classes. See Dig Methods.
Given symbol or string argument name
, returns the object that is specified by name
and identifiers
:
Foo = Struct.new(:a) f = Foo.new(Foo.new({b: [1, 2, 3]})) f.dig(:a) # => #<struct Foo a={:b=>[1, 2, 3]}> f.dig(:a, :a) # => {:b=>[1, 2, 3]} f.dig(:a, :a, :b) # => [1, 2, 3] f.dig(:a, :a, :b, 0) # => 1 f.dig(:b, 0) # => nil
Given integer argument n
, returns the object that is specified by n
and identifiers
:
f.dig(0) # => #<struct Foo a={:b=>[1, 2, 3]}> f.dig(0, 0) # => {:b=>[1, 2, 3]} f.dig(0, 0, :b) # => [1, 2, 3] f.dig(0, 0, :b, 0) # => 1 f.dig(:b, 0) # => nil
Equivalent to self.to_s.encoding
; see String#encoding
.
Returns the birth time for the file. If the platform doesn’t have birthtime, raises NotImplementedError
.
See File.birthtime
.
Creates a pair of sockets connected each other.
domain should be a communications domain such as: :INET, :INET6, :UNIX, etc.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain, defaults to 0 for the domain.
s1, s2 = Socket.pair(:UNIX, :STREAM, 0) s1.send "a", 0 s1.send "b", 0 s1.close p s2.recv(10) #=> "ab" p s2.recv(10) #=> "" p s2.recv(10) #=> "" s1, s2 = Socket.pair(:UNIX, :DGRAM, 0) s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "a" p s2.recv(10) #=> "b"
Creates a pair of sockets connected each other.
domain should be a communications domain such as: :INET, :INET6, :UNIX, etc.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain, defaults to 0 for the domain.
s1, s2 = Socket.pair(:UNIX, :STREAM, 0) s1.send "a", 0 s1.send "b", 0 s1.close p s2.recv(10) #=> "ab" p s2.recv(10) #=> "" p s2.recv(10) #=> "" s1, s2 = Socket.pair(:UNIX, :DGRAM, 0) s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "a" p s2.recv(10) #=> "b"
Creates a pair of sockets connected to each other.
type should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain. 0 is default protocol for the domain.
s1, s2 = UNIXSocket.pair s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "ab"