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
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.
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.
require 'io/console' IO::console.getpass("Enter password:") Enter password: # => "mypassword"
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
Get the internal timeout duration or nil if it was not set.
Sets the internal timeout to the specified duration or nil. The timeout applies to all blocking operations where possible.
When the operation performs longer than the timeout set, IO::TimeoutError
is raised.
This affects the following methods (but is not limited to): gets
, puts
, read
, write
, wait_readable
and wait_writable
. This also affects blocking socket operations like Socket#accept
and Socket#connect
.
Some operations like File#open
and IO#close
are not affected by the timeout. A timeout during a write operation may leave the IO
in an inconsistent state, e.g. data was partially written. Generally speaking, a timeout is a last ditch effort to prevent an application from hanging on slow I/O operations, such as those that occur during a slowloris attack.
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 up to maxlen
bytes from the stream; returns a string (either a new string or the given out_string
). Its encoding is:
The unchanged encoding of out_string
, if out_string
is given.
ASCII-8BIT, otherwise.
Contains maxlen
bytes from the stream, if available.
Otherwise contains all available bytes, if any available.
Otherwise is an empty string.
With the single non-negative integer argument maxlen
given, returns a new string:
f = File.new('t.txt') f.readpartial(20) # => "First line\nSecond l" f.readpartial(20) # => "ine\n\nFourth line\n" f.readpartial(20) # => "Fifth line\n" f.readpartial(20) # Raises EOFError. f.close
With both argument maxlen
and string argument out_string
given, returns modified out_string
:
f = File.new('t.txt') s = 'foo' f.readpartial(20, s) # => "First line\nSecond l" s = 'bar' f.readpartial(0, s) # => "" f.close
This method is useful for a stream such as a pipe, a socket, or a tty. It blocks only when no data is immediately available. This means that it blocks only when all of the following are true:
The byte buffer in the stream is empty.
The content of the stream is empty.
The stream is not at EOF.
When blocked, the method waits for either more data or EOF on the stream:
If more data is read, the method returns the data.
If EOF is reached, the method raises EOFError
.
When not blocked, the method responds immediately:
Returns data from the buffer if there is any.
Otherwise returns data from the stream if there is any.
Otherwise raises EOFError
if the stream has reached EOF.
Note that this method is similar to sysread. The differences are:
If the byte buffer is not empty, read from the byte buffer instead of “sysread for buffered IO
(IOError
)”.
It doesn’t cause Errno::EWOULDBLOCK and Errno::EINTR. When readpartial meets EWOULDBLOCK and EINTR by read system call, readpartial retries the system call.
The latter means that readpartial is non-blocking-flag insensitive. It blocks on the situation IO#sysread
causes Errno::EWOULDBLOCK as if the fd is blocking mode.
Examples:
# # Returned Buffer Content Pipe Content r, w = IO.pipe # w << 'abc' # "" "abc". r.readpartial(4096) # => "abc" "" "" r.readpartial(4096) # (Blocks because buffer and pipe are empty.) # # Returned Buffer Content Pipe Content r, w = IO.pipe # w << 'abc' # "" "abc" w.close # "" "abc" EOF r.readpartial(4096) # => "abc" "" EOF r.readpartial(4096) # raises EOFError # # Returned Buffer Content Pipe Content r, w = IO.pipe # w << "abc\ndef\n" # "" "abc\ndef\n" r.gets # => "abc\n" "def\n" "" w << "ghi\n" # "def\n" "ghi\n" r.readpartial(4096) # => "def\n" "" "ghi\n" r.readpartial(4096) # => "ghi\n" "" ""
Returns true
if the stream is associated with a terminal device (tty), false
otherwise:
f = File.new('t.txt').isatty #=> false f.close f = File.new('/dev/tty').isatty #=> true f.close
Returns true
if object
is an element of self
, false
otherwise:
(1..4).include?(2) # => true (1..4).include?(5) # => false (1..4).include?(4) # => true (1...4).include?(4) # => false ('a'..'d').include?('b') # => true ('a'..'d').include?('e') # => false ('a'..'d').include?('B') # => false ('a'..'d').include?('d') # => true ('a'...'d').include?('d') # => false
If begin and end are numeric, include?
behaves like cover?
(1..3).include?(1.5) # => true (1..3).cover?(1.5) # => true
But when not numeric, the two methods may differ:
('a'..'d').include?('cc') # => false ('a'..'d').cover?('cc') # => true
Related: Range#cover?
.
Returns true
if rat
is less than 0.
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
With no block given, returns the MatchData
object that describes the match, if any, or nil
if none; the search begins at the given character offset
in string
:
/abra/.match('abracadabra') # => #<MatchData "abra"> /abra/.match('abracadabra', 4) # => #<MatchData "abra"> /abra/.match('abracadabra', 8) # => nil /abra/.match('abracadabra', 800) # => nil string = "\u{5d0 5d1 5e8 5d0}cadabra" /abra/.match(string, 7) #=> #<MatchData "abra"> /abra/.match(string, 8) #=> nil /abra/.match(string.b, 8) #=> #<MatchData "abra">
With a block given, calls the block if and only if a match is found; returns the block’s value:
/abra/.match('abracadabra') {|matchdata| p matchdata } # => #<MatchData "abra"> /abra/.match('abracadabra', 4) {|matchdata| p matchdata } # => #<MatchData "abra"> /abra/.match('abracadabra', 8) {|matchdata| p matchdata } # => nil /abra/.match('abracadabra', 8) {|marchdata| fail 'Cannot happen' } # => nil
Output (from the first two blocks above):
#<MatchData "abra"> #<MatchData "abra"> /(.)(.)(.)/.match("abc")[2] # => "b" /(.)(.)/.match("abc", 1)[2] # => "c"
Returns true
or false
to indicate whether the regexp is matched or not without updating $~ and other related variables. If the second parameter is present, it specifies the position in the string to begin the search.
/R.../.match?("Ruby") # => true /R.../.match?("Ruby", 1) # => false /P.../.match?("Ruby") # => false $& # => nil
It returns the timeout interval for Regexp
matching in second. nil
means no default timeout configuration.
This configuration is per-object. The global configuration set by Regexp.timeout=
is ignored if per-object configuration is set.
re = Regexp.new("^a*b?a*$", timeout: 1) re.timeout #=> 1.0 re =~ "a" * 100000 + "x" #=> regexp match timeout (RuntimeError)
It returns the current default timeout interval for Regexp
matching in second. nil
means no default timeout configuration.
It sets the default timeout interval for Regexp
matching in second. nil
means no default timeout configuration. This configuration is process-global. If you want to set timeout for each Regexp
, use timeout
keyword for Regexp.new
.
Regexp.timeout = 1 /^a*b?a*$/ =~ "a" * 100000 + "x" #=> regexp match timeout (RuntimeError)
Returns a new set that is a copy of the set, flattening each containing set recursively.
Equivalent to Set#flatten
, but replaces the receiver with the result in place. Returns nil if no modifications were made.
Merges the elements of the given enumerable objects to the set and returns self.
Returns the member names of the Struct
descendant as an array:
Customer = Struct.new(:name, :address, :zip) Customer.members # => [:name, :address, :zip]
Returns the number of members.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.size #=> 3
Returns the member names from self
as an array:
Customer = Struct.new(:name, :address, :zip) Customer.new.members # => [:name, :address, :zip]
Related: to_a
.