Returns true if the date is on or after the day of calendar reform.
Date.new(1582,10,15).gregorian? #=> true (Date.new(1582,10,15) - 1).gregorian? #=> false
Returns the Julian day number denoting the day of calendar reform.
Date.new(2001,2,3).start #=> 2299161.0 Date.new(2001,2,3,Date::GREGORIAN).start #=> -Infinity
This method is equivalent to new_start
(Date::ENGLAND
).
This method is equivalent to new_start
(Date::GREGORIAN
).
Iterates evaluation of the given block, which takes a date object. The limit should be a date object.
Date.new(2001).step(Date.new(2001,-1,-1)).select{|d| d.sunday?}.size #=> 52
Returns the value as a string for inspection.
Date.new(2001,2,3).inspect #=> "#<Date: 2001-02-03>" DateTime.new(2001,2,3,4,5,6,'-7').inspect #=> "#<DateTime: 2001-02-03T04:05:06-07:00>"
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 ...>
Returns a detailed string representing time. Unlike to_s
, preserves subsecond in the representation for easier debugging.
t = Time.now t.inspect #=> "2012-11-10 18:16:12.261257655 +0100" t.strftime "%Y-%m-%d %H:%M:%S.%N %z" #=> "2012-11-10 18:16:12.261257655 +0100" t.utc.inspect #=> "2012-11-10 17:16:12.261257655 UTC" t.strftime "%Y-%m-%d %H:%M:%S.%N UTC" #=> "2012-11-10 17:16:12.261257655 UTC"
Returns the minute of the hour (0..59) for time.
t = Time.now #=> 2007-11-19 08:25:51 -0600 t.min #=> 25
Returns true
if time occurs during Daylight Saving Time
in its time zone.
# CST6CDT: Time.local(2000, 1, 1).zone #=> "CST" Time.local(2000, 1, 1).isdst #=> false Time.local(2000, 1, 1).dst? #=> false Time.local(2000, 7, 1).zone #=> "CDT" Time.local(2000, 7, 1).isdst #=> true Time.local(2000, 7, 1).dst? #=> true # Asia/Tokyo: Time.local(2000, 1, 1).zone #=> "JST" Time.local(2000, 1, 1).isdst #=> false Time.local(2000, 1, 1).dst? #=> false Time.local(2000, 7, 1).zone #=> "JST" Time.local(2000, 7, 1).isdst #=> false Time.local(2000, 7, 1).dst? #=> false
Returns true
if time occurs during Daylight Saving Time
in its time zone.
# CST6CDT: Time.local(2000, 1, 1).zone #=> "CST" Time.local(2000, 1, 1).isdst #=> false Time.local(2000, 1, 1).dst? #=> false Time.local(2000, 7, 1).zone #=> "CDT" Time.local(2000, 7, 1).isdst #=> true Time.local(2000, 7, 1).dst? #=> true # Asia/Tokyo: Time.local(2000, 1, 1).zone #=> "JST" Time.local(2000, 1, 1).isdst #=> false Time.local(2000, 1, 1).dst? #=> false Time.local(2000, 7, 1).zone #=> "JST" Time.local(2000, 7, 1).isdst #=> false Time.local(2000, 7, 1).dst? #=> false
Returns true
if time represents Friday.
t = Time.local(1987, 12, 18) #=> 1987-12-18 00:00:00 -0600 t.friday? #=> true
Returns a string representation of self
:
Customer = Struct.new(:name, :address, :zip) # => Customer joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.inspect # => "#<struct Customer name=\"Joe Smith\", address=\"123 Maple, Anytown NC\", zip=12345>"
Struct#to_s
is an alias for Struct#inspect
.
Returns the number of members.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.size #=> 3
Struct#length
is an alias for Struct#size
.
Tries to set console size. The effect depends on the platform and the running environment.
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
Reads the entire file specified by name as individual lines, and returns those lines in an array. Lines are separated by sep.
If name
starts with a pipe character ("|"
) and the receiver is the IO
class, a subprocess is created in the same way as Kernel#open
, and its output is returned. Consider to use File.readlines
to disable the behavior of subprocess invocation.
a = File.readlines("testfile") a[0] #=> "This is line one\n" b = File.readlines("testfile", chomp: true) b[0] #=> "This is line one" IO.readlines("|ls -a") #=> [".\n", "..\n", ...]
If the last argument is a hash, it’s the keyword argument to open.
The options hash accepts the following keys:
When the optional chomp
keyword argument has a true value, \n
, \r
, and \r\n
will be removed from the end of each line.
See also IO.read
for details about name
and open_args.
Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file). binread ensures the file is closed before returning. The open mode would be "rb:ASCII-8BIT"
.
If name
starts with a pipe character ("|"
) and the receiver is the IO
class, a subprocess is created in the same way as Kernel#open
, and its output is returned. Consider to use File.binread
to disable the behavior of subprocess invocation.
File.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n" File.binread("testfile", 20) #=> "This is line one\nThi" File.binread("testfile", 20, 10) #=> "ne one\nThis is line " IO.binread("| cat testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
See also IO.read
for details about name
and open_args.
Opens the file, optionally seeks to the given offset, writes string, then returns the length written. write
ensures the file is closed before returning. If offset is not given in write mode, the file is truncated. Otherwise, it is not truncated.
If name
starts with a pipe character ("|"
) and the receiver is the IO
class, a subprocess is created in the same way as Kernel#open
, and its output is returned. Consider to use File.write
to disable the behavior of subprocess invocation.
File.write("testfile", "0123456789", 20) #=> 10 # File could contain: "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n" File.write("testfile", "0123456789") #=> 10 # File would now read: "0123456789" IO.write("|tr a-z A-Z", "abc") #=> 3 # Prints "ABC" to the standard output
If the last argument is a hash, it specifies options for the internal open(). It accepts the following keys:
string or encoding
Specifies the encoding of the read string. See Encoding.aliases
for possible encodings.
string or integer
Specifies the mode argument for open(). It must start with “w”, “a”, or “r+”, otherwise it will cause an error. See IO.new
for the list of possible modes.
integer
Specifies the perm argument for open().
array
Specifies arguments for open() as an array. This key can not be used in combination with other keys.
See also IO.read
for details about name
and open_args.
Writes the given string to ios using a low-level write. Returns the number of bytes written. Do not mix with other methods that write to ios or you may get unpredictable results. Raises SystemCallError
on error.
f = File.new("out", "w") f.syswrite("ABCDEF") #=> 6
Writes the given string to ios at offset using pwrite() system call. This is advantageous to combining IO#seek
and IO#write
in that it is atomic, allowing multiple threads/process to share the same IO
object for reading the file at various locations. This bypasses any userspace buffering of the IO
layer. Returns the number of bytes written. Raises SystemCallError
on error and NotImplementedError
if platform does not implement the system call.
File.open("out", "w") do |f| f.pwrite("ABCDEF", 3) #=> 6 end File.read("out") #=> "\u0000\u0000\u0000ABCDEF"