Changes this process’s idea of the file system root. Only a privileged process may make this call. Not available on all platforms. On Unix systems, see chroot(2)
for more information.
Locks or unlocks a file according to locking_constant (a logical or of the values in the table below). Returns false
if File::LOCK_NB is specified and the operation would otherwise have blocked. Not available on all platforms.
Locking constants (in class File
):
LOCK_EX | Exclusive lock. Only one process may hold an | exclusive lock for a given file at a time. ----------+------------------------------------------------ LOCK_NB | Don't block when locking. May be combined | with other lock options using logical or. ----------+------------------------------------------------ LOCK_SH | Shared lock. Multiple processes may each hold a | shared lock for a given file at the same time. ----------+------------------------------------------------ LOCK_UN | Unlock.
Example:
# update a counter using write lock # don't use "w" because it truncates the file before lock. File.open("counter", File::RDWR|File::CREAT, 0644) {|f| f.flock(File::LOCK_EX) value = f.read.to_i + 1 f.rewind f.write("#{value}\n") f.flush f.truncate(f.pos) } # read the counter using read lock File.open("counter", "r") {|f| f.flock(File::LOCK_SH) p f.read }
Returns true
if the named file exists and has a zero size.
file_name can be an IO
object.
Invokes Module.prepend_features
on each parameter in reverse order.
Callback invoked whenever the receiver is included in another module or class. This should be used in preference to Module.append_features
if your code wants to perform some action when a module is included in another.
module A def A.included(mod) puts "#{self} included in #{mod}" end end module Enumerable include A end # => prints "A included in Enumerable"
With no arguments, sets the default visibility for subsequently defined methods to private. With arguments, sets the named methods to have private visibility. String
arguments are converted to symbols.
module Mod def a() end def b() end private def c() end private :a end Mod.private_instance_methods #=> [:a, :c]
Note that to show a private method on RDoc
, use :doc:
.
Returns an Array
of two Integer
values.
The first value is the current number of significant digits in the BigDecimal
. The second value is the maximum number of significant digits for the BigDecimal
.
BigDecimal('5').precs #=> [9, 18]
Round to the nearest integer (by default), returning the result as a BigDecimal
if n is specified, or as an Integer
if it isn’t.
BigDecimal('3.14159').round #=> 3 BigDecimal('8.7').round #=> 9 BigDecimal('-9.9').round #=> -10 BigDecimal('3.14159').round(2).class.name #=> "BigDecimal" BigDecimal('3.14159').round.class.name #=> "Integer"
If n is specified and positive, the fractional part of the result has no more than that many digits.
If n is specified and negative, at least that many digits to the left of the decimal point will be 0 in the result.
BigDecimal('3.14159').round(3) #=> 3.142 BigDecimal('13345.234').round(-2) #=> 13300.0
The value of the optional mode argument can be used to determine how rounding is performed; see BigDecimal.mode
.
Returns True if the value is zero.
Returns self if the value is non-zero, nil otherwise.
Returns rat
rounded to the nearest value 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).round #=> 3 Rational(2, 3).round #=> 1 Rational(-3, 2).round #=> -2 # decimal - 1 2 3 . 4 5 6 # ^ ^ ^ ^ ^ ^ # precision -3 -2 -1 0 +1 +2 Rational('-123.456').round(+1).to_f #=> -123.5 Rational('-123.456').round(-1) #=> -120
The optional half
keyword argument is available similar to Float#round
.
Rational(25, 100).round(1, half: :up) #=> (3/10) Rational(25, 100).round(1, half: :down) #=> (1/5) Rational(25, 100).round(1, half: :even) #=> (1/5) Rational(35, 100).round(1, half: :up) #=> (2/5) Rational(35, 100).round(1, half: :down) #=> (3/10) Rational(35, 100).round(1, half: :even) #=> (2/5) Rational(-25, 100).round(1, half: :up) #=> (-3/10) Rational(-25, 100).round(1, half: :down) #=> (-1/5) Rational(-25, 100).round(1, half: :even) #=> (-1/5)
Same as Time::gm
, but interprets the values in the local time zone.
Time.local(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 -0600
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 local time (using the local time zone in effect for this process).
If utc_offset
is given, it is used instead of the local time. utc_offset
can be given as a human-readable string (eg. "+09:00"
) or as a number of seconds (eg. 32400
).
t = Time.utc(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC t.utc? #=> true l = t.getlocal #=> 2000-01-01 14:15:01 -0600 l.utc? #=> false t == l #=> true j = t.getlocal("+09:00") #=> 2000-01-02 05:15:01 +0900 j.utc? #=> false t == j #=> true k = t.getlocal(9*60*60) #=> 2000-01-02 05:15:01 +0900 k.utc? #=> false t == k #=> true
Rounds sub seconds 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.
require 'time' t = Time.utc(2010,3,30, 5,43,25.123456789r) t.iso8601(10) #=> "2010-03-30T05:43:25.1234567890Z" t.round.iso8601(10) #=> "2010-03-30T05:43:25.0000000000Z" t.round(0).iso8601(10) #=> "2010-03-30T05:43:25.0000000000Z" t.round(1).iso8601(10) #=> "2010-03-30T05:43:25.1000000000Z" t.round(2).iso8601(10) #=> "2010-03-30T05:43:25.1200000000Z" t.round(3).iso8601(10) #=> "2010-03-30T05:43:25.1230000000Z" t.round(4).iso8601(10) #=> "2010-03-30T05:43:25.1235000000Z" t = Time.utc(1999,12,31, 23,59,59) (t + 0.4).round.iso8601(3) #=> "1999-12-31T23:59:59.000Z" (t + 0.49).round.iso8601(3) #=> "1999-12-31T23:59:59.000Z" (t + 0.5).round.iso8601(3) #=> "2000-01-01T00:00:00.000Z" (t + 1.4).round.iso8601(3) #=> "2000-01-01T00:00:00.000Z" (t + 1.49).round.iso8601(3) #=> "2000-01-01T00:00:00.000Z" (t + 1.5).round.iso8601(3) #=> "2000-01-01T00:00:01.000Z" t = Time.utc(1999,12,31, 23,59,59) (t + 0.123456789).round(4).iso8601(6) #=> "1999-12-31T23:59:59.123500Z"
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.
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.
Reads maxlen bytes from ios using the pread system call and returns them as a string without modifying the underlying descriptor offset. This is advantageous compared to combining IO#seek
and IO#read
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. If the optional outbuf argument is present, it must reference a String
, which will receive the data. Raises SystemCallError
on error, EOFError
at end of file and NotImplementedError
if platform does not implement the system call.
File.write("testfile", "This is line one\nThis is line two\n") File.open("testfile") do |f| p f.read # => "This is line one\nThis is line two\n" p f.pread(12, 0) # => "This is line" p f.pread(9, 8) # => "line one\n" end
Provides a mechanism for issuing low-level commands to control or query I/O devices. Arguments and results are platform dependent. If arg is a number, its value is passed directly. If it is a string, it is interpreted as a binary sequence of bytes. On Unix platforms, see ioctl(2)
for details. Not implemented on all platforms.