Returns the integer hour of the day for self
, in range (0..23):
t = Time.new(2000, 1, 2, 3, 4, 5, 6) # => 2000-01-02 03:04:05 +000006 t.hour # => 3
Returns true
if self
represents a Thursday, false
otherwise:
t = Time.utc(2000, 1, 6) # => 2000-01-06 00:00:00 UTC t.thursday? # => true
Related: Time#friday?
, Time#saturday?
, Time#sunday?
.
Returns true
if self
represents a Friday, false
otherwise:
t = Time.utc(2000, 1, 7) # => 2000-01-07 00:00:00 UTC t.friday? # => true
Related: Time#saturday?
, Time#sunday?
, Time#monday?
.
Returns true
if self
represents a Saturday, false
otherwise:
t = Time.utc(2000, 1, 1) # => 2000-01-01 00:00:00 UTC t.saturday? # => true
Related: Time#sunday?
, Time#monday?
, Time#tuesday?
.
Returns the current cursor position as a two-element array of integers (row, column)
io.cursor # => [3, 5]
You must require ‘io/console’ to use this method.
Opens the stream, writes the given data
to it, and closes the stream; returns the number of bytes written.
When called from class IO (but not subclasses of IO), this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
The first argument must be a string that is the path to a file.
With only argument path
given, writes the given data
to the file at that path:
IO.write('t.tmp', 'abc') # => 3 File.read('t.tmp') # => "abc"
If offset
is zero (the default), the file is overwritten:
IO.write('t.tmp', 'A') # => 1 File.read('t.tmp') # => "A"
If offset
in within the file content, the file is partly overwritten:
IO.write('t.tmp', 'abcdef') # => 3 File.read('t.tmp') # => "abcdef" # Offset within content. IO.write('t.tmp', '012', 2) # => 3 File.read('t.tmp') # => "ab012f"
If offset
is outside the file content, the file is padded with null characters "\u0000"
:
IO.write('t.tmp', 'xyz', 10) # => 3 File.read('t.tmp') # => "ab012f\u0000\u0000\u0000\u0000xyz"
Optional keyword arguments opts
specify:
Encoding options.
Behaves like IO.write
, except that the stream is opened in binary mode with ASCII-8BIT encoding.
When called from class IO (but not subclasses of IO), this method has potential security vulnerabilities if called with untrusted input; see Command Injection.
Writes the given objects to the stream; returns nil
. Appends the output record separator $OUTPUT_RECORD_SEPARATOR
($\
), if it is not nil
. See Line IO.
With argument objects
given, for each object:
Converts via its method to_s
if not a string.
Writes to the stream.
If not the last object, writes the output field separator $OUTPUT_FIELD_SEPARATOR
($,
) if it is not nil
.
With default separators:
f = File.open('t.tmp', 'w+') objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero'] p $OUTPUT_RECORD_SEPARATOR p $OUTPUT_FIELD_SEPARATOR f.print(*objects) f.rewind p f.read f.close
Output:
nil nil "00.00/10+0izerozero"
With specified separators:
$\ = "\n" $, = ',' f.rewind f.print(*objects) f.rewind p f.read
Output:
"0,0.0,0/1,0+0i,zero,zero\n"
With no argument given, writes the content of $_
(which is usually the most recent user input):
f = File.open('t.tmp', 'w+') gets # Sets $_ to the most recent user input. f.print f.close
Formats and writes objects
to the stream.
For details on format_string
, see Format Specifications.
Writes the given object
to self, which must be opened for writing (see Modes); returns the number bytes written. If object
is not a string is converted via method to_s:
f = File.new('t.tmp', 'w') f.syswrite('foo') # => 3 f.syswrite(30) # => 2 f.syswrite(:foo) # => 3 f.close
This methods should not be used with other stream-writer methods.
Behaves like IO#write
, except that it:
Writes at the given offset
(in bytes).
Disregards, and does not modify, the stream’s position (see Position).
Bypasses any user space buffering in the stream.
Because this method does not disturb the stream’s state (its position, in particular), pwrite
allows multiple threads and processes to use the same IO object for writing at various offsets.
f = File.open('t.tmp', 'w+') # Write 6 bytes at offset 3. f.pwrite('ABCDEF', 3) # => 6 f.rewind f.read # => "\u0000\u0000\u0000ABCDEF" f.close
Not available on some platforms.
Writes each of the given objects
to self
, which must be opened for writing (see Access Modes); returns the total number bytes written; each of objects
that is not a string is converted via method to_s
:
$stdout.write('Hello', ', ', 'World!', "\n") # => 14 $stdout.write('foo', :bar, 2, "\n") # => 8
Output:
Hello, World! foobar2
Related: IO#read
.
Returns an array containing the elements in self
, if a finite collection; raises an exception otherwise.
(1..4).to_a # => [1, 2, 3, 4] (1...4).to_a # => [1, 2, 3] ('a'..'d').to_a # => ["a", "b", "c", "d"]
Returns the original string of self
:
/ab+c/ix.source # => "ab+c"
Regexp
escape sequences are retained:
/\x20\+/.source # => "\\x20\\+"
Lexer escape characters are not retained:
/\//.source # => "/"
Callback invoked whenever a subclass of the current class is created.
Example:
class Foo def self.inherited(subclass) puts "New subclass: #{subclass}" end end class Bar < Foo end class Baz < Bar end
produces:
New subclass: Bar New subclass: Baz
Return the entries (files and subdirectories) in the directory, each as a Pathname
object.
The results contains just the names in the directory, without any trailing slashes or recursive look-up.
pp Pathname.new('/usr/local').entries #=> [#<Pathname:share>, # #<Pathname:lib>, # #<Pathname:..>, # #<Pathname:include>, # #<Pathname:etc>, # #<Pathname:bin>, # #<Pathname:man>, # #<Pathname:games>, # #<Pathname:.>, # #<Pathname:sbin>, # #<Pathname:src>]
The result may contain the current directory #<Pathname:.>
and the parent directory #<Pathname:..>
.
If you don’t want .
and ..
and want directories, consider Pathname#children
.
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
.
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 underlying string:
StringIO.open('foo') do |strio| p strio.string strio.string = 'bar' p strio.string end
Output:
"foo" "bar"
Related: StringIO#string=
(assigns the underlying string).
Assigns the underlying string as other_string
, and sets position to zero; returns other_string
:
StringIO.open('foo') do |strio| p strio.string strio.string = 'bar' p strio.string end
Output:
"foo" "bar"
Related: StringIO#string
(returns the underlying string).