Returns the value raised to the power of n.
Note that n must be an Integer
.
Also available as the operator **.
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.
Creates a date object denoting the given week date.
The week and the day of week should be a negative or a positive number (as a relative week/day from the end of year/week when negative). They should not be zero.
Date.commercial(2001) #=> #<Date: 2001-01-01 ...> Date.commercial(2002) #=> #<Date: 2001-12-31 ...> Date.commercial(2001,5,6) #=> #<Date: 2001-02-03 ...>
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 ...>
Returns a new Time object based the on given arguments; its timezone is the local timezone.
In the first form (up to seven arguments), argument year
is required.
Time.local(2000) # => 2000-01-01 00:00:00 -0600 Time.local(0, 1, 2, 3, 4, 5, 6.5) # => 0000-01-02 03:04:05.0000065 -0600
In the second form, all ten arguments are required, though the last four are ignored. This form is useful for creating a time from a 10-element array such as those returned by to_a
.
array = Time.now.to_a p array # => [57, 26, 13, 24, 4, 2021, 6, 114, true, "Central Daylight Time"] array[5] = 2000 Time.local(*array) # => 2000-04-24 13:26:57 -0500
Parameters:
year
: an integer year.
month
: a month value, which may be:
An integer month in the range 1..12
.
A 3-character string that matches regular expression /jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/i
.
day
: an integer day in the range 1..31
(less than 31 for some months).
hour
: an integer hour in the range 0..23
.
min
: an integer minute in the range 0..59
.
isec_i
is the integer number of seconds in the range 0..60
.
usec
is the number of microseconds (Integer
, Float
, or Rational
) in the range 0..1000000
.
Alias: Time.mktime.
Related: Time.utc
.
Converts time to local time (using the local time zone in effect at the creation time of time) modifying the receiver.
If utc_offset
is given, it is used instead of the local time.
t = Time.utc(2000, "jan", 1, 20, 15, 1) #=> 2000-01-01 20:15:01 UTC t.utc? #=> true t.localtime #=> 2000-01-01 14:15:01 -0600 t.utc? #=> false t.localtime("+09:00") #=> 2000-01-02 05:15:01 +0900 t.utc? #=> false
If utc_offset
is not given and time is local time, just returns the receiver.
Returns a new Time
object representing time in UTC.
t = Time.local(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 -0600 t.gmt? #=> false y = t.getgm #=> 2000-01-02 02:15:01 UTC y.gmt? #=> true t == y #=> true
Returns a new Time
object representing time in UTC.
t = Time.local(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 -0600 t.gmt? #=> false y = t.getgm #=> 2000-01-02 02:15:01 UTC y.gmt? #=> true t == y #=> true
Floors subsecond to a given precision in decimal digits (0 digits by default). It returns a new Time
object. ndigits
should be zero or a positive integer.
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(0) #=> 2010-03-30 05:43:25 UTC t.floor(1) #=> 2010-03-30 05:43:25.1 UTC t.floor(2) #=> 2010-03-30 05:43:25.12 UTC t.floor(3) #=> 2010-03-30 05:43:25.123 UTC t.floor(4) #=> 2010-03-30 05:43:25.1234 UTC t = Time.utc(1999,12,31, 23,59,59) (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 t = Time.utc(1999,12,31, 23,59,59) (t + 0.123456789).floor(4) #=> 1999-12-31 23:59:59.1234 UTC
Returns the member names of the Struct
descendant as an array:
Customer = Struct.new(:name, :address, :zip) Customer.members # => [:name, :address, :zip]
With a block given, returns an array of values from self
for which the block returns a truthy value:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) a = joe.select {|value| value.is_a?(String) } a # => ["Joe Smith", "123 Maple, Anytown NC"] a = joe.select {|value| value.is_a?(Integer) } a # => [12345]
With no block given, returns an Enumerator
.
Struct#filter
is an alias for Struct#select
.
Returns the member names from self
as an array:
Customer = Struct.new(:name, :address, :zip) Customer.new.members # => [:name, :address, :zip]
Related: to_a
.
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
.
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 data from the stream; assigns the return value to $_
.
With no arguments given, returns the next line as determined by line separator $/
, or nil
if none:
f = File.open('t.txt') f.gets # => "This is line one.\n" $_ # => "This is line one.\n" f.gets # => "This is the second line.\n" f.gets # => "This is the third line.\n" f.gets # => nil
With string argument sep
given, but not argument limit
, returns the next line as determined by line separator sep
, or nil
if none:
f = File.open('t.txt') f.gets(' is') # => "This is" f.gets(' is') # => " line one.\nThis is" f.gets(' is') # => " the second line.\nThis is" f.gets(' is') # => " the third line.\n" f.gets(' is') # => nil
Note two special values for sep
:
nil
: The entire stream is read and returned.
''
(empty string): The next “paragraph” is read and returned, the paragraph separator being two successive line separators.
With integer argument limit
given, returns up to limit+1
bytes:
# Text with 1-byte characters. File.open('t.txt') {|f| f.gets(1) } # => "T" File.open('t.txt') {|f| f.gets(2) } # => "Th" File.open('t.txt') {|f| f.gets(3) } # => "Thi" File.open('t.txt') {|f| f.gets(4) } # => "This" # No more than one line. File.open('t.txt') {|f| f.gets(17) } # => "This is line one." File.open('t.txt') {|f| f.gets(18) } # => "This is line one.\n" File.open('t.txt') {|f| f.gets(19) } # => "This is line one.\n" # Text with 2-byte characters, which will not be split. File.open('t.rus') {|f| f.gets(1).size } # => 1 File.open('t.rus') {|f| f.gets(2).size } # => 1 File.open('t.rus') {|f| f.gets(3).size } # => 2 File.open('t.rus') {|f| f.gets(4).size } # => 2
With arguments sep
and limit
, combines the two behaviors above:
Returns the next line as determined by line separator sep
, or nil
if none.
But returns no more than limit+1
bytes.
For all forms above, trailing optional keyword arguments may be given; see Getline Options:
f = File.open('t.txt') # Chomp the lines. f.gets(chomp: true) # => "This is line one." f.gets(chomp: true) # => "This is the second line." f.gets(chomp: true) # => "This is the third line." f.gets(chomp: true) # => nil
Reads a one-character string from ios. Returns nil
if called at end of file.
f = File.new("testfile") f.getc #=> "h" f.getc #=> "e"
Gets the next 8-bit byte (0..255) from ios. Returns nil
if called at end of file.
f = File.new("testfile") f.getbyte #=> 84 f.getbyte #=> 104