Returns a hash of values parsed from string
, which should be a valid HTTP date format:
d = Date.new(2001, 2, 3) s = d.httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT" Date._httpdate(s) # => {:wday=>6, :mday=>3, :mon=>2, :year=>2001, :hour=>0, :min=>0, :sec=>0, :zone=>"GMT", :offset=>0}
Related: Date.httpdate
(returns a Date object).
Returns a new Date object with values parsed from string
, which should be a valid HTTP date format:
d = Date.new(2001, 2, 3) s = d.httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT" Date.httpdate(s) # => #<Date: 2001-02-03>
See:
Argument start.
Argument limit.
Related: Date._httpdate
(returns a hash).
Returns true
if self
is a Saturday, false
otherwise.
Equivalent to strftime
with argument '%a, %d %b %Y %T GMT'
; see Formats for Dates and Times:
Date.new(2001, 2, 3).httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT"
Creates a new DateTime
object by parsing from a string according to some RFC 2616 format.
DateTime.httpdate('Sat, 03 Feb 2001 04:05:06 GMT') #=> #<DateTime: 2001-02-03T04:05:06+00:00 ...>
Raise an ArgumentError
when the string length is longer than limit. You can stop this check by passing limit: nil
, but note that it may take a long time to parse.
Parses date
as an HTTP-date defined by RFC 2616 and converts it to a Time
object.
ArgumentError
is raised if date
is not compliant with RFC 2616 or if the Time
class cannot represent specified date.
See httpdate
for more information on this format.
require 'time' Time.httpdate("Thu, 06 Oct 2011 02:26:12 GMT") #=> 2011-10-06 02:26:12 UTC
You must require ‘time’ to use this method.
Returns a string which represents the time as RFC 1123 date of HTTP-date defined by RFC 2616:
day-of-week, DD month-name CCYY hh:mm:ss GMT
Note that the result is always UTC (GMT).
require 'time' t = Time.now t.httpdate # => "Thu, 06 Oct 2011 02:26:12 GMT"
You must require ‘time’ to use this method.
Returns a new Time
object based the on given arguments, in the UTC timezone.
With one to seven arguments given, the arguments are interpreted as in the first calling sequence above:
Time.utc(year, month = 1, mday = 1, hour = 0, min = 0, sec = 0, usec = 0)
Examples:
Time.utc(2000) # => 2000-01-01 00:00:00 UTC Time.utc(-2000) # => -2000-01-01 00:00:00 UTC
There are no minimum and maximum values for the required argument year
.
For the optional arguments:
month
: Month in range (1..12), or case-insensitive 3-letter month name:
Time.utc(2000, 1) # => 2000-01-01 00:00:00 UTC Time.utc(2000, 12) # => 2000-12-01 00:00:00 UTC Time.utc(2000, 'jan') # => 2000-01-01 00:00:00 UTC Time.utc(2000, 'JAN') # => 2000-01-01 00:00:00 UTC
mday
: Month day in range(1..31):
Time.utc(2000, 1, 1) # => 2000-01-01 00:00:00 UTC Time.utc(2000, 1, 31) # => 2000-01-31 00:00:00 UTC
hour
: Hour in range (0..23), or 24 if min
, sec
, and usec
are zero:
Time.utc(2000, 1, 1, 0) # => 2000-01-01 00:00:00 UTC Time.utc(2000, 1, 1, 23) # => 2000-01-01 23:00:00 UTC Time.utc(2000, 1, 1, 24) # => 2000-01-02 00:00:00 UTC
min
: Minute in range (0..59):
Time.utc(2000, 1, 1, 0, 0) # => 2000-01-01 00:00:00 UTC Time.utc(2000, 1, 1, 0, 59) # => 2000-01-01 00:59:00 UTC
sec
: Second in range (0..59), or 60 if usec
is zero:
Time.utc(2000, 1, 1, 0, 0, 0) # => 2000-01-01 00:00:00 UTC Time.utc(2000, 1, 1, 0, 0, 59) # => 2000-01-01 00:00:59 UTC Time.utc(2000, 1, 1, 0, 0, 60) # => 2000-01-01 00:01:00 UTC
usec
: Microsecond in range (0..999999):
Time.utc(2000, 1, 1, 0, 0, 0, 0) # => 2000-01-01 00:00:00 UTC Time.utc(2000, 1, 1, 0, 0, 0, 999999) # => 2000-01-01 00:00:00.999999 UTC
The values may be:
Integers, as above.
Numerics convertible to integers:
Time.utc(Float(0.0), Rational(1, 1), 1.0, 0.0, 0.0, 0.0, 0.0) # => 0000-01-01 00:00:00 UTC
String
integers:
a = %w[0 1 1 0 0 0 0 0] # => ["0", "1", "1", "0", "0", "0", "0", "0"] Time.utc(*a) # => 0000-01-01 00:00:00 UTC
When exactly ten arguments are given, the arguments are interpreted as in the second calling sequence above:
Time.utc(sec, min, hour, mday, month, year, dummy, dummy, dummy, dummy)
where the dummy
arguments are ignored:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Time.utc(*a) # => 0005-04-03 02:01:00 UTC
This form is useful for creating a Time
object from a 10-element array returned by Time.to_a
:
t = Time.new(2000, 1, 2, 3, 4, 5, 6) # => 2000-01-02 03:04:05 +000006 a = t.to_a # => [5, 4, 3, 2, 1, 2000, 0, 2, false, nil] Time.utc(*a) # => 2000-01-02 03:04:05 UTC
The two forms have their first six arguments in common, though in different orders; the ranges of these common arguments are the same for both forms; see above.
Raises an exception if the number of arguments is eight, nine, or greater than ten.
Related: Time.local
.
Returns self
, converted to the UTC timezone:
t = Time.new(2000) # => 2000-01-01 00:00:00 -0600 t.utc? # => false t.utc # => 2000-01-01 06:00:00 UTC t.utc? # => true
Related: Time#getutc
(returns a new converted Time
object).
Returns a new Time
object representing the value of self
converted to the UTC timezone:
local = Time.local(2000) # => 2000-01-01 00:00:00 -0600 local.utc? # => false utc = local.getutc # => 2000-01-01 06:00:00 UTC utc.utc? # => true utc == local # => true
Returns true
if self
represents a time in UTC (GMT):
now = Time.now # => 2022-08-18 10:24:13.5398485 -0500 now.utc? # => false utc = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC utc.utc? # => true
Related: Time.utc
.
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 a new Time
object based on the given arguments.
Required argument time
may be either of:
A Time
object, whose value is the basis for the returned time; also influenced by optional keyword argument in:
(see below).
A numeric number of Epoch seconds for the returned time.
Examples:
t = Time.new(2000, 12, 31, 23, 59, 59) # => 2000-12-31 23:59:59 -0600 secs = t.to_i # => 978328799 Time.at(secs) # => 2000-12-31 23:59:59 -0600 Time.at(secs + 0.5) # => 2000-12-31 23:59:59.5 -0600 Time.at(1000000000) # => 2001-09-08 20:46:40 -0500 Time.at(0) # => 1969-12-31 18:00:00 -0600 Time.at(-1000000000) # => 1938-04-24 17:13:20 -0500
Optional numeric argument subsec
and optional symbol argument units
work together to specify subseconds for the returned time; argument units
specifies the units for subsec
:
:millisecond
: subsec
in milliseconds:
Time.at(secs, 0, :millisecond) # => 2000-12-31 23:59:59 -0600 Time.at(secs, 500, :millisecond) # => 2000-12-31 23:59:59.5 -0600 Time.at(secs, 1000, :millisecond) # => 2001-01-01 00:00:00 -0600 Time.at(secs, -1000, :millisecond) # => 2000-12-31 23:59:58 -0600
:microsecond
or :usec
: subsec
in microseconds:
Time.at(secs, 0, :microsecond) # => 2000-12-31 23:59:59 -0600 Time.at(secs, 500000, :microsecond) # => 2000-12-31 23:59:59.5 -0600 Time.at(secs, 1000000, :microsecond) # => 2001-01-01 00:00:00 -0600 Time.at(secs, -1000000, :microsecond) # => 2000-12-31 23:59:58 -0600
:nanosecond
or :nsec
: subsec
in nanoseconds:
Time.at(secs, 0, :nanosecond) # => 2000-12-31 23:59:59 -0600 Time.at(secs, 500000000, :nanosecond) # => 2000-12-31 23:59:59.5 -0600 Time.at(secs, 1000000000, :nanosecond) # => 2001-01-01 00:00:00 -0600 Time.at(secs, -1000000000, :nanosecond) # => 2000-12-31 23:59:58 -0600
Optional keyword argument in: zone
specifies the timezone for the returned time:
Time.at(secs, in: '+12:00') # => 2001-01-01 17:59:59 +1200 Time.at(secs, in: '-12:00') # => 2000-12-31 17:59:59 -1200
For the forms of argument zone
, see Timezone Specifiers.
Returns pathname configuration variable using fpathconf().
name should be a constant under Etc
which begins with PC_
.
The return value is an integer or nil. nil means indefinite limit. (fpathconf() returns -1 but errno is not set.)
require 'etc' IO.pipe {|r, w| p w.pathconf(Etc::PC_PIPE_BUF) #=> 4096 }
Enables/disables echo back. On some platforms, all combinations of this flags and raw/cooked mode may not be valid.
You must require ‘io/console’ to use this method.
Yields self
with disabling echo back.
STDIN.noecho(&:gets)
will read and return a line without echo back.
You must require ‘io/console’ to use this method.
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
Calls the block with each successive line read from the stream.
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, parses lines from the file at the given path
, as determined by the default line separator, and calls the block with each successive line:
File.foreach('t.txt') {|line| p line }
Output: the same as above.
For both forms, command and path, the remaining arguments are the same.
With argument sep
given, parses lines as determined by that line separator (see Line Separator):
File.foreach('t.txt', 'li') {|line| p line }
Output:
"First li" "ne\nSecond li" "ne\n\nThird li" "ne\nFourth li" "ne\n"
Each paragraph:
File.foreach('t.txt', '') {|paragraph| p paragraph }
Output:
"First line\nSecond line\n\n" "Third line\nFourth line\n"
With argument limit
given, parses lines as determined by the default line separator and the given line-length limit (see Line Separator and Line Limit):
File.foreach('t.txt', 7) {|line| p line }
Output:
"First l" "ine\n" "Second " "line\n" "\n" "Third l" "ine\n" "Fourth l" "line\n"
With arguments sep
and limit
given, combines the two behaviors (see Line Separator and Line Limit).
Optional keyword arguments opts
specify:
Encoding options.
Returns an Enumerator
if no block is given.
Writes a character to the stream. See Character IO.
If object
is numeric, converts to integer if necessary, then writes the character whose code is the least significant byte; if object
is a string, writes the first character:
$stdout.putc "A" $stdout.putc 65
Output:
AA
Calls the block with each remaining line read from the stream; returns self
. Does nothing if already at end-of-stream; See Line IO.
With no arguments given, reads lines as determined by line separator $/
:
f = File.new('t.txt') f.each_line {|line| p line } f.each_line {|line| fail 'Cannot happen' } f.close
Output:
"First line\n" "Second line\n" "\n" "Fourth line\n" "Fifth line\n"
With only string argument sep
given, reads lines as determined by line separator sep
; see Line Separator:
f = File.new('t.txt') f.each_line('li') {|line| p line } f.close
Output:
"First li" "ne\nSecond li" "ne\n\nFourth li" "ne\nFifth li" "ne\n"
The two special values for sep
are honored:
f = File.new('t.txt') # Get all into one string. f.each_line(nil) {|line| p line } f.close
Output:
"First line\nSecond line\n\nFourth line\nFifth line\n" f.rewind # Get paragraphs (up to two line separators). f.each_line('') {|line| p line }
Output:
"First line\nSecond line\n\n" "Fourth line\nFifth line\n"
With only integer argument limit
given, limits the number of bytes in each line; see Line Limit:
f = File.new('t.txt') f.each_line(8) {|line| p line } f.close
Output:
"First li" "ne\n" "Second l" "ine\n" "\n" "Fourth l" "ine\n" "Fifth li" "ne\n"
With arguments sep
and limit
given, combines the two behaviors (see Line Separator and Line Limit).
Optional keyword argument chomp
specifies whether line separators are to be omitted:
f = File.new('t.txt') f.each_line(chomp: true) {|line| p line } f.close
Output:
"First line" "Second line" "" "Fourth line" "Fifth line"
Returns an Enumerator
if no block is given.
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 and returns the next 1-character string from the stream; returns nil
if already at end-of-stream. See Character IO.
f = File.open('t.txt') f.getc # => "F" f.close f = File.open('t.rus') f.getc.ord # => 1090 f.close
Related: IO#readchar
(may raise EOFError
).
Reads and returns the next 1-character string from the stream; raises EOFError
if already at end-of-stream. See Character IO.
f = File.open('t.txt') f.readchar # => "F" f.close f = File.open('t.rus') f.readchar.ord # => 1090 f.close
Pushes back (“unshifts”) the given data onto the stream’s buffer, placing the data so that it is next to be read; returns nil
. See Character IO.
Note that:
Calling the method has no effect with unbuffered reads (such as IO#sysread
).
Calling rewind
on the stream discards the pushed-back data.
When argument integer
is given, interprets the integer as a character:
File.write('t.tmp', '012') f = File.open('t.tmp') f.ungetc(0x41) # => nil f.read # => "A012" f.rewind f.ungetc(0x0442) # => nil f.getc.ord # => 1090 f.close
When argument string
is given, uses all characters:
File.write('t.tmp', '012') f = File.open('t.tmp') f.ungetc('A') # => nil f.read # => "A012" f.rewind f.ungetc("\u0442\u0435\u0441\u0442") # => nil f.getc.ord # => 1090 f.getc.ord # => 1077 f.getc.ord # => 1089 f.getc.ord # => 1090 f.close