Returns True if the value is zero.
Returns self if the value is non-zero, nil otherwise.
The coerce method provides support for Ruby type coercion. It is not enabled by default.
This means that binary operations like + * / or - can often be performed on a BigDecimal
and an object of another type, if the other object can be coerced into a BigDecimal
value.
e.g.
a = BigDecimal("1.0") b = a / 2.0 #=> 0.5
Note that coercing a String
to a BigDecimal
is not supported by default; it requires a special compile-time option when building Ruby.
Returns the numerator.
Rational(7).numerator #=> 7 Rational(7, 1).numerator #=> 7 Rational(9, -4).numerator #=> -9 Rational(-2, -10).numerator #=> 1
Returns the largest number less than or equal to rat
with 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).floor #=> 3 Rational(2, 3).floor #=> 0 Rational(-3, 2).floor #=> -2 # decimal - 1 2 3 . 4 5 6 # ^ ^ ^ ^ ^ ^ # precision -3 -2 -1 0 +1 +2 Rational('-123.456').floor(+1).to_f #=> -123.5 Rational('-123.456').floor(-1) #=> -130
This method is an alias for http_header
, when HTML5
tag maker is inactive.
NOTE: use http_header
to create HTTP header blocks, this alias is only provided for backwards compatibility.
Using header
with the HTML5
tag maker will create a <header> element.
Returns a new Date object constructed from the arguments.
Argument cwyear
gives the year, and should be an integer.
Argument cweek
gives the index of the week within the year, and should be in range (1..53) or (-53..-1); in some years, 53 or -53 will be out-of-range; if negative, counts backward from the end of the year:
Date.commercial(2022, 1, 1).to_s # => "2022-01-03" Date.commercial(2022, 52, 1).to_s # => "2022-12-26"
Argument cwday
gives the indes of the weekday within the week, and should be in range (1..7) or (-7..-1); 1 or -7 is Monday; if negative, counts backward from the end of the week:
Date.commercial(2022, 1, 1).to_s # => "2022-01-03" Date.commercial(2022, 1, -7).to_s # => "2022-01-03"
When cweek
is 1:
If January 1 is a Friday, Saturday, or Sunday, the first week begins in the week after:
Date::ABBR_DAYNAMES[Date.new(2023, 1, 1).wday] # => "Sun" Date.commercial(2023, 1, 1).to_s # => "2023-01-02" Date.commercial(2023, 1, 7).to_s # => "2023-01-08"
Otherwise, the first week is the week of January 1, which may mean some of the days fall on the year before:
Date::ABBR_DAYNAMES[Date.new(2020, 1, 1).wday] # => "Wed" Date.commercial(2020, 1, 1).to_s # => "2019-12-30" Date.commercial(2020, 1, 7).to_s # => "2020-01-05"
See argument start.
Related: Date.jd
, Date.new
, Date.ordinal
.
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 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
Time#getgm
is an alias for Time#getutc
.
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
Time#getgm
is an alias for Time#getutc
.
Returns a new Time object whose numerical value is less than or equal to self
with its seconds truncated to precision ndigits
:
t = Time.utc(2010, 3, 30, 5, 43, 25.123456789r) t # => 2010-03-30 05:43:25.123456789 UTC t.floor # => 2010-03-30 05:43:25 UTC t.floor(2) # => 2010-03-30 05:43:25.12 UTC t.floor(4) # => 2010-03-30 05:43:25.1234 UTC t.floor(6) # => 2010-03-30 05:43:25.123456 UTC t.floor(8) # => 2010-03-30 05:43:25.12345678 UTC t.floor(10) # => 2010-03-30 05:43:25.123456789 UTC t = Time.utc(1999, 12, 31, 23, 59, 59) t # => 1999-12-31 23:59:59 UTC (t + 0.4).floor # => 1999-12-31 23:59:59 UTC (t + 0.9).floor # => 1999-12-31 23:59:59 UTC (t + 1.4).floor # => 2000-01-01 00:00:00 UTC (t + 1.9).floor # => 2000-01-01 00:00:00 UTC
Related: Time#ceil
, Time#round
.
Reads and returns a character in raw mode.
See IO#raw
for details on the parameters.
You must require ‘io/console’ to use this method.
Reads and returns a line without echo back. Prints prompt
unless it is nil
.
The newline character that terminates the read line is removed from the returned string, see String#chomp!
.
You must require ‘io/console’ to use this method.
Returns true
if an IO
object is in non-blocking mode.
Enables non-blocking mode on a stream when set to true
, and blocking mode when set to false
.
This method set or clear O_NONBLOCK flag for the file descriptor in ios.
The behavior of most IO
methods is not affected by this flag because they retry system calls to complete their task after EAGAIN and partial read/write. (An exception is IO#syswrite
which doesn’t retry.)
This method can be used to clear non-blocking mode of standard I/O. Since nonblocking methods (read_nonblock
, etc.) set non-blocking mode but they doesn’t clear it, this method is usable as follows.
END { STDOUT.nonblock = false } STDOUT.write_nonblock("foo")
Since the flag is shared across processes and many non-Ruby commands doesn’t expect standard I/O with non-blocking mode, it would be safe to clear the flag before Ruby program exits.
For example following Ruby program leaves STDIN/STDOUT/STDER non-blocking mode. (STDIN, STDOUT and STDERR are connected to a terminal. So making one of them nonblocking-mode effects other two.) Thus cat command try to read from standard input and it causes “Resource temporarily unavailable” error (EAGAIN).
% ruby -e ' STDOUT.write_nonblock("foo\n")'; cat foo cat: -: Resource temporarily unavailable
Clearing the flag makes the behavior of cat command normal. (cat command waits input from standard input.)
% ruby -rio/nonblock -e ' END { STDOUT.nonblock = false } STDOUT.write_nonblock("foo") '; cat foo
Yields self
in non-blocking mode.
When false
is given as an argument, self
is yielded in blocking mode. The original mode is restored after the block is executed.
Reads and returns a line from the stream; assigns the return value to $_
. See Line IO.
With no arguments given, returns the next line as determined by line separator $/
, or nil
if none:
f = File.open('t.txt') f.gets # => "First line\n" $_ # => "First line\n" f.gets # => "\n" f.gets # => "Fourth line\n" f.gets # => "Fifth line\n" f.gets # => nil f.close
With only string argument sep
given, returns the next line as determined by line separator sep
, or nil
if none; see Line Separator:
f = File.new('t.txt') f.gets('l') # => "First l" f.gets('li') # => "ine\nSecond li" f.gets('lin') # => "ne\n\nFourth lin" f.gets # => "e\n" f.close
The two special values for sep
are honored:
f = File.new('t.txt') # Get all. f.gets(nil) # => "First line\nSecond line\n\nFourth line\nFifth line\n" f.rewind # Get paragraph (up to two line separators). f.gets('') # => "First line\nSecond line\n\n" f.close
With only integer argument limit
given, limits the number of bytes in the line; see Line Limit:
# No more than one line. File.open('t.txt') {|f| f.gets(10) } # => "First line" File.open('t.txt') {|f| f.gets(11) } # => "First line\n" File.open('t.txt') {|f| f.gets(12) } # => "First line\n"
With arguments sep
and limit
given, combines the two behaviors:
Returns the next line as determined by line separator sep
, or nil
if none.
But returns no more bytes than are allowed by the limit.
Optional keyword argument chomp
specifies whether line separators are to be omitted:
f = File.open('t.txt') # Chomp the lines. f.gets(chomp: true) # => "First line" f.gets(chomp: true) # => "Second line" f.gets(chomp: true) # => "" f.gets(chomp: true) # => "Fourth line" f.gets(chomp: true) # => "Fifth line" f.gets(chomp: true) # => nil f.close
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 byte (in range 0..255) from the stream; returns nil
if already at end-of-stream. See Byte IO.
f = File.open('t.txt') f.getbyte # => 70 f.close f = File.open('t.rus') f.getbyte # => 209 f.close
Related: IO#readbyte
(may raise EOFError
).
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 Byte 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, uses only its low-order byte:
File.write('t.tmp', '012') f = File.open('t.tmp') f.ungetbyte(0x41) # => nil f.read # => "A012" f.rewind f.ungetbyte(0x4243) # => nil f.read # => "C012" f.close
When argument string
is given, uses all bytes:
File.write('t.tmp', '012') f = File.open('t.tmp') f.ungetbyte('A') # => nil f.read # => "A012" f.rewind f.ungetbyte('BCDE') # => nil f.read # => "BCDE012" 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
Closes the stream for both reading and writing if open for either or both; returns nil
. See Open and Closed Streams.
If the stream is open for writing, flushes any buffered writes to the operating system before closing.
If the stream was opened by IO.popen
, sets global variable $?
(child exit status).
Example:
IO.popen('ruby', 'r+') do |pipe| puts pipe.closed? pipe.close puts $? puts pipe.closed? end
Output:
false pid 13760 exit 0 true
Related: IO#close_read
, IO#close_write
, IO#closed?
.
Returns true
if the stream is closed for both reading and writing, false
otherwise. See Open and Closed Streams.
IO.popen('ruby', 'r+') do |pipe| puts pipe.closed? pipe.close_read puts pipe.closed? pipe.close_write puts pipe.closed? end
Output:
false false true
Related: IO#close_read
, IO#close_write
, IO#close
.