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 ...>
Creates a DateTime
object denoting the given week date.
DateTime.commercial(2001) #=> #<DateTime: 2001-01-01T00:00:00+00:00 ...> DateTime.commercial(2002) #=> #<DateTime: 2001-12-31T00:00:00+00:00 ...> DateTime.commercial(2001,5,6,4,5,6,'+7') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
Like Time.utc
, except that the returned Time
object has the local timezone, not the UTC timezone:
# With seven arguments. Time.local(0, 1, 2, 3, 4, 5, 6) # => 0000-01-02 03:04:05.000006 -0600 # With exactly ten arguments. Time.local(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) # => 0005-04-03 02:01:00 -0600
With no argument given:
Returns self
if self
is a local time.
Otherwise returns a new Time
in the user’s local timezone:
t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC t.localtime # => 2000-01-01 14:15:01 -0600
With argument zone
given, returns the new Time
object created by converting self
to the given time zone:
t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC t.localtime("-09:00") # => 2000-01-01 11:15:01 -0900
For forms of argument zone
, see Timezone Specifiers.
Returns a new Time
object representing the value of self
converted to a given timezone; if zone
is nil
, the local timezone is used:
t = Time.utc(2000) # => 2000-01-01 00:00:00 UTC t.getlocal # => 1999-12-31 18:00:00 -0600 t.getlocal('+12:00') # => 2000-01-01 12:00:00 +1200
For forms of argument zone
, see Timezone Specifiers.
Returns status information for ios as an object of type File::Stat
.
f = File.new("testfile") s = f.stat "%o" % s.mode #=> "100644" s.blksize #=> 4096 s.atime #=> Wed Apr 09 08:53:54 CDT 2003
Immediately writes to disk all data buffered in the stream, via the operating system’s: fdatasync(2)
, if supported, otherwise via fsync(2)
, if supported; otherwise raises an exception.
Reads up to maxlen
bytes from the stream; returns a string (either a new string or the given out_string
). Its encoding is:
The unchanged encoding of out_string
, if out_string
is given.
ASCII-8BIT, otherwise.
Contains maxlen
bytes from the stream, if available.
Otherwise contains all available bytes, if any available.
Otherwise is an empty string.
With the single non-negative integer argument maxlen
given, returns a new string:
f = File.new('t.txt') f.readpartial(20) # => "First line\nSecond l" f.readpartial(20) # => "ine\n\nFourth line\n" f.readpartial(20) # => "Fifth line\n" f.readpartial(20) # Raises EOFError. f.close
With both argument maxlen
and string argument out_string
given, returns modified out_string
:
f = File.new('t.txt') s = 'foo' f.readpartial(20, s) # => "First line\nSecond l" s = 'bar' f.readpartial(0, s) # => "" f.close
This method is useful for a stream such as a pipe, a socket, or a tty. It blocks only when no data is immediately available. This means that it blocks only when all of the following are true:
The byte buffer in the stream is empty.
The content of the stream is empty.
The stream is not at EOF.
When blocked, the method waits for either more data or EOF on the stream:
If more data is read, the method returns the data.
If EOF is reached, the method raises EOFError
.
When not blocked, the method responds immediately:
Returns data from the buffer if there is any.
Otherwise returns data from the stream if there is any.
Otherwise raises EOFError
if the stream has reached EOF.
Note that this method is similar to sysread. The differences are:
If the byte buffer is not empty, read from the byte buffer instead of “sysread for buffered IO
(IOError
)”.
It doesn’t cause Errno::EWOULDBLOCK and Errno::EINTR. When readpartial meets EWOULDBLOCK and EINTR by read system call, readpartial retries the system call.
The latter means that readpartial is non-blocking-flag insensitive. It blocks on the situation IO#sysread
causes Errno::EWOULDBLOCK as if the fd is blocking mode.
Examples:
# # Returned Buffer Content Pipe Content r, w = IO.pipe # w << 'abc' # "" "abc". r.readpartial(4096) # => "abc" "" "" r.readpartial(4096) # (Blocks because buffer and pipe are empty.) # # Returned Buffer Content Pipe Content r, w = IO.pipe # w << 'abc' # "" "abc" w.close # "" "abc" EOF r.readpartial(4096) # => "abc" "" EOF r.readpartial(4096) # raises EOFError # # Returned Buffer Content Pipe Content r, w = IO.pipe # w << "abc\ndef\n" # "" "abc\ndef\n" r.gets # => "abc\n" "def\n" "" w << "ghi\n" # "def\n" "ghi\n" r.readpartial(4096) # => "def\n" "" "ghi\n" r.readpartial(4096) # => "ghi\n" "" ""
Returns the current position (in bytes) in self
(see Position):
f = File.open('t.txt') f.tell # => 0 f.gets # => "First line\n" f.tell # => 12 f.close
Returns a simpler approximation of the value if the optional argument eps
is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
r = Rational(5033165, 16777216) r.rationalize #=> (5033165/16777216) r.rationalize(Rational('0.01')) #=> (3/10) r.rationalize(Rational('0.1')) #=> (1/3)
Replaces the elements with ones returned by collect()
. Returns an enumerator if no block is given.
Returns the values in self
as an array:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
Related: members
.
Returns the real (absolute) pathname for self
in the actual filesystem.
Does not contain symlinks or useless dots, ..
and .
.
All components of the pathname must exist when this method is called.
Returns the real (absolute) pathname of self
in the actual filesystem.
Does not contain symlinks or useless dots, ..
and .
.
The last component of the real pathname can be nonexistent.
See File.lstat
.
enable the socket option IPV6_V6ONLY
if IPV6_V6ONLY
is available.
Obtains address information for nodename:servname.
Note that Addrinfo.getaddrinfo
provides the same functionality in an object oriented style.
family should be an address family such as: :INET, :INET6, etc.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the family, and defaults to 0 for the family.
flags should be bitwise OR of Socket::AI_* constants.
Socket.getaddrinfo("www.ruby-lang.org", "http", nil, :STREAM) #=> [["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68", 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP Socket.getaddrinfo("localhost", nil) #=> [["AF_INET", 0, "localhost", "127.0.0.1", 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP
reverse_lookup directs the form of the third element, and has to be one of below. If reverse_lookup is omitted, the default value is nil
.
+true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time. +false+, +:numeric+: hostname is the same as numeric address. +nil+: obey to the current +do_not_reverse_lookup+ flag.
If Addrinfo
object is preferred, use Addrinfo.getaddrinfo
.
Lookups the IP address of host.
require 'socket' IPSocket.getaddress("localhost") #=> "127.0.0.1" IPSocket.getaddress("ip6-localhost") #=> "::1"
returns a list of addrinfo objects as an array.
This method converts nodename (hostname) and service (port) to addrinfo. Since the conversion is not unique, the result is a list of addrinfo objects.
nodename or service can be nil if no conversion intended.
family, socktype and protocol are hint for preferred protocol. If the result will be used for a socket with SOCK_STREAM, SOCK_STREAM should be specified as socktype. If so, Addrinfo.getaddrinfo
returns addrinfo list appropriate for SOCK_STREAM. If they are omitted or nil is given, the result is not restricted.
Similarly, PF_INET6 as family restricts for IPv6.
flags should be bitwise OR of Socket::AI_??? constants such as follows. Note that the exact list of the constants depends on OS.
AI_PASSIVE Get address to use with bind() AI_CANONNAME Fill in the canonical name AI_NUMERICHOST Prevent host name resolution AI_NUMERICSERV Prevent service name resolution AI_V4MAPPED Accept IPv4-mapped IPv6 addresses AI_ALL Allow all addresses AI_ADDRCONFIG Accept only if any address is assigned
Note that socktype should be specified whenever application knows the usage of the address. Some platform causes an error when socktype is omitted and servname is specified as an integer because some port numbers, 512 for example, are ambiguous without socktype.
Addrinfo.getaddrinfo("www.kame.net", 80, nil, :STREAM) #=> [#<Addrinfo: 203.178.141.194:80 TCP (www.kame.net)>, # #<Addrinfo: [2001:200:dff:fff1:216:3eff:feb1:44d7]:80 TCP (www.kame.net)>]
returns the address family as an integer.
Addrinfo.tcp("localhost", 80).afamily == Socket::AF_INET #=> true
returns the protocol family as an integer.
Addrinfo.tcp("localhost", 80).pfamily == Socket::PF_INET #=> true
Returns the current position (in bytes); see Position.