Returns rat
truncated (toward zero) to a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns a rational when ndigits
is positive, otherwise returns an integer.
Rational(3).truncate #=> 3 Rational(2, 3).truncate #=> 0 Rational(-3, 2).truncate #=> -1 # decimal - 1 2 3 . 4 5 6 # ^ ^ ^ ^ ^ ^ # precision -3 -2 -1 0 +1 +2 Rational('-123.456').truncate(+1).to_f #=> -123.4 Rational('-123.456').truncate(-1) #=> -120
Synonym for $stdin.
Synonym for $stdout.
Print an argument or list of arguments to the default output stream
cgi = CGI.new cgi.print # default: cgi.print == $DEFAULT_OUTPUT.print
Returns true if the date is Friday.
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::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 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 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
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.
Same as IO.write
except opening the file in binary mode and ASCII-8BIT encoding ("wb: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.binwrite
to disable the behavior of subprocess invocation.
See also IO.read
for details about name
and open_args.
Creates a pair of pipe endpoints (connected to each other) and returns them as a two-element array of IO
objects: [
read_io, write_io ]
.
If a block is given, the block is called and returns the value of the block. read_io and write_io are sent to the block as arguments. If read_io and write_io are not closed when the block exits, they are closed. i.e. closing read_io and/or write_io doesn’t cause an error.
Not available on all platforms.
If an encoding (encoding name or encoding object) is specified as an optional argument, read string from pipe is tagged with the encoding specified. If the argument is a colon separated two encoding names “A:B”, the read string is converted from encoding A (external encoding) to encoding B (internal encoding), then tagged with B. If two optional arguments are specified, those must be encoding objects or encoding names, and the first one is the external encoding, and the second one is the internal encoding. If the external encoding and the internal encoding is specified, optional hash argument specify the conversion option.
In the example below, the two processes close the ends of the pipe that they are not using. This is not just a cosmetic nicety. The read end of a pipe will not generate an end of file condition if there are any writers with the pipe still open. In the case of the parent process, the rd.read
will never return if it does not first issue a wr.close
.
rd, wr = IO.pipe if fork wr.close puts "Parent got: <#{rd.read}>" rd.close Process.wait else rd.close puts "Sending message to parent" wr.write "Hi Dad" wr.close end
produces:
Sending message to parent Parent got: <Hi Dad>
Writes the given object(s) to ios. Returns nil
.
The stream must be opened for writing. Each given object that isn’t a string will be converted by calling its to_s
method. When called without arguments, prints the contents of $_
.
If the output field separator ($,
) is not nil
, it is inserted between objects. If the output record separator ($\
) is not nil
, it is appended to the output.
$stdout.print("This is ", 100, " percent.\n")
produces:
This is 100 percent.
Formats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf
for details.
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"
Writes each of the given objects
to self
, which must be opened for writing (see 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
Iterates over the elements of self
.
With a block given and no argument, calls the block each element of the range; returns self
:
a = [] (1..5).step {|element| a.push(element) } # => 1..5 a # => [1, 2, 3, 4, 5] a = [] ('a'..'e').step {|element| a.push(element) } # => "a".."e" a # => ["a", "b", "c", "d", "e"]
With a block given and a positive integer argument n
given, calls the block with element 0
, element n
, element 2n
, and so on:
a = [] (1..5).step(2) {|element| a.push(element) } # => 1..5 a # => [1, 3, 5] a = [] ('a'..'e').step(2) {|element| a.push(element) } # => "a".."e" a # => ["a", "c", "e"]
With no block given, returns an enumerator, which will be of class Enumerator::ArithmeticSequence
if self
is numeric; otherwise of class Enumerator:
e = (1..5).step(2) # => ((1..5).step(2)) e.class # => Enumerator::ArithmeticSequence ('a'..'e').step # => #<Enumerator: ...>
Related: Range#%
.
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
With no argument, returns the last element of self
, if it exists:
(1..4).last # => 4 ('a'..'d').last # => "d"
Note that last
with no argument returns the end element of self
even if exclude_end?
is true
:
(1...4).last # => 4 ('a'...'d').last # => "d"
With non-negative integer argument n
given, returns the last n
elements in an array:
(1..10).last(3) # => [8, 9, 10] (1..10).last(0) # => [] (1..4).last(50) # => [1, 2, 3, 4]
Note that last
with argument does not return the end element of self
if exclude_end?
it true
:
(1...4).last(3) # => [1, 2, 3] ('a'...'d').last(3) # => ["a", "b", "c"]
Raises an exception if there is no last element:
(1..).last # Raises RangeError
Deletes every element that appears in the given enumerable object and returns self.