Returns the string representation of the path
File.path("/dev/null") #=> "/dev/null" File.path(Pathname.new("/tmp")) #=> "/tmp"
Same as IO#stat
, but does not follow the last symbolic link. Instead, reports on the link itself.
File.symlink("testfile", "link2test") #=> 0 File.stat("testfile").size #=> 66 f = File.new("link2test") f.lstat.size #=> 8 f.stat.size #=> 66
Returns the last access time (a Time
object) for file, or epoch if file has not been accessed.
File.new("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
Changes permission bits on file to the bit pattern represented by mode_int. Actual effects are platform dependent; on Unix systems, see chmod(2)
for details. Follows symbolic links. Also see File#lchmod.
f = File.new("out", "w"); f.chmod(0644) #=> 0
Changes the owner and group of file to the given numeric owner and group id’s. Only a process with superuser privileges may change the owner of a file. The current owner of a file may change the file’s group to any group to which the owner belongs. A nil
or -1 owner or group id is ignored. Follows symbolic links. See also File#lchown.
File.new("testfile").chown(502, 1000)
Truncates file to at most integer bytes. The file must be opened for writing. Not available on all platforms.
f = File.new("out", "w") f.syswrite("1234567890") #=> 10 f.truncate(5) #=> 0 f.close() #=> nil File.size("out") #=> 5
Returns the pathname used to create file as a string. Does not normalize the name.
The pathname may not point to the file corresponding to file. For instance, the pathname becomes void when the file has been moved or deleted.
This method raises IOError
for a file created using File::Constants::TMPFILE because they don’t have a pathname.
File.new("testfile").path #=> "testfile" File.new("/tmp/../tmp/xxx", "w").path #=> "/tmp/../tmp/xxx"
Returns a replicated encoding of enc whose name is name. The new encoding should have the same byte structure of enc. If name is used by another encoding, raise ArgumentError
.
Checks the compatibility of two objects.
If the objects are both strings they are compatible when they are concatenatable. The encoding of the concatenated string will be returned if they are compatible, nil if they are not.
Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b") #=> #<Encoding:ISO-8859-1> Encoding.compatible?( "\xa1".force_encoding("iso-8859-1"), "\xa1\xa1".force_encoding("euc-jp")) #=> nil
If the objects are non-strings their encodings are compatible when they have an encoding and:
Either encoding is US-ASCII compatible
One of the encodings is a 7-bit encoding
Iterates over the block according to how this Enumerator
was constructed. If no block and no arguments are given, returns self.
"Hello, world!".scan(/\w+/) #=> ["Hello", "world"] "Hello, world!".to_enum(:scan, /\w+/).to_a #=> ["Hello", "world"] "Hello, world!".to_enum(:scan).each(/\w+/).to_a #=> ["Hello", "world"] obj = Object.new def obj.each_arg(a, b=:b, *rest) yield a yield b yield rest :method_returned end enum = obj.to_enum :each_arg, :a, :x enum.each.to_a #=> [:a, :x, []] enum.each.equal?(enum) #=> true enum.each { |elm| elm } #=> :method_returned enum.each(:y, :z).to_a #=> [:a, :x, [:y, :z]] enum.each(:y, :z).equal?(enum) #=> false enum.each(:y, :z) { |elm| elm } #=> :method_returned
Return the status value associated with this system exit.
The first form is equivalent to attr_reader
. The second form is equivalent to attr_accessor(name)
but deprecated. The last form is equivalent to attr_reader(name)
but deprecated.
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 the remainder from dividing by the value.
x.remainder(y) means x-y*(x/y).truncate
Truncate to the nearest integer (by default), returning the result as a BigDecimal
.
BigDecimal('3.14159').truncate #=> 3 BigDecimal('8.7').truncate #=> 8 BigDecimal('-9.9').truncate #=> -9
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').truncate(3) #=> 3.141 BigDecimal('13345.234').truncate(-2) #=> 13300.0
Returns the numerator.
Rational(7).numerator #=> 7 Rational(7, 1).numerator #=> 7 Rational(9, -4).numerator #=> -9 Rational(-2, -10).numerator #=> 1
Returns the denominator (always positive).
Rational(7).denominator #=> 1 Rational(7, 1).denominator #=> 1 Rational(9, -4).denominator #=> 4 Rational(-2, -10).denominator #=> 5
Returns true
if rat
is less than 0.
Returns the absolute value of rat
.
(1/2r).abs #=> (1/2) (-1/2r).abs #=> (1/2)
Rational#magnitude
is an alias for Rational#abs
.
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
Returns a simpler approximation of the value if the optional argument eps
is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
r = Rational(5033165, 16777216) r.rationalize #=> (5033165/16777216) r.rationalize(Rational('0.01')) #=> (3/10) r.rationalize(Rational('0.1')) #=> (1/3)
Returns a hash of parsed elements.
Raise an ArgumentError
when the string length is longer than limit. You can stop this check by passing ‘limit: nil`, but note that it may take a long time to parse.
Creates a new Date
object by parsing from a string according to some RFC 2616 format.
Date.httpdate('Sat, 03 Feb 2001 00:00:00 GMT') #=> #<Date: 2001-02-03 ...>
Raise an ArgumentError
when the string length is longer than limit. You can stop this check by passing ‘limit: nil`, but note that it may take a long time to parse.
Returns true if the date is Saturday.