Returns a copy of the receiver with trailing whitespace removed; see Whitespace in Strings:
whitespace = "\x00\t\n\v\f\r " s = whitespace + 'abc' + whitespace s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r " s.rstrip # => "\u0000\t\n\v\f\r abc"
Related: String#lstrip
, String#strip
.
Like String#strip
, except that any modifications are made in self
; returns self
if any modification are made, nil
otherwise.
Related: String#lstrip!
, String#strip!
.
Like String#lstrip
, except that any modifications are made in self
; returns self
if any modification are made, nil
otherwise.
Related: String#rstrip!
, String#strip!
.
Like String#rstrip
, except that any modifications are made in self
; returns self
if any modification are made, nil
otherwise.
Related: String#lstrip!
, String#strip!
.
Returns a string containing a representation of self
; depending of the value of self
, the string representation may contain:
A fixed-point number.
3.14.to_s # => "3.14"
A number in “scientific notation” (containing an exponent).
(10.1**50).to_s # => "1.644631821843879e+50"
‘Infinity’.
(10.1**500).to_s # => "Infinity"
‘-Infinity’.
(-10.1**500).to_s # => "-Infinity"
‘NaN’ (indicating not-a-number).
(0.0/0.0).to_s # => "NaN"
Returns self
truncated to an Integer
.
1.2.to_i # => 1 (-1.2).to_i # => -1
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).to_i # => 2 (!)
Returns a float or integer that is a “floor” value for self
, as specified by ndigits
, which must be an integer-convertible object.
When self
is zero, returns a zero value: a float if ndigits
is positive, an integer otherwise:
f = 0.0 # => 0.0 f.floor(20) # => 0.0 f.floor(0) # => 0 f.floor(-20) # => 0
When self
is non-zero and ndigits
is positive, returns a float with ndigits
digits after the decimal point (as available):
f = 12345.6789 f.floor(1) # => 12345.6 f.floor(3) # => 12345.678 f.floor(30) # => 12345.6789 f = -12345.6789 f.floor(1) # => -12345.7 f.floor(3) # => -12345.679 f.floor(30) # => -12345.6789
When self
is non-zero and ndigits
is non-positive, returns an integer value based on a computed granularity:
The granularity is 10 ** ndigits.abs
.
The returned value is the largest multiple of the granularity that is less than or equal to self
.
Examples with positive self
:
ndigits | Granularity | 12345.6789.floor(ndigits) |
---|---|---|
0 | 1 | 12345 |
-1 | 10 | 12340 |
-2 | 100 | 12300 |
-3 | 1000 | 12000 |
-4 | 10000 | 10000 |
-5 | 100000 | 0 |
Examples with negative self
:
ndigits | Granularity | -12345.6789.floor(ndigits) |
---|---|---|
0 | 1 | -12346 |
-1 | 10 | -12350 |
-2 | 100 | -12400 |
-3 | 1000 | -13000 |
-4 | 10000 | -20000 |
-5 | 100000 | -100000 |
-6 | 1000000 | -1000000 |
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).floor # => 2 # Not 3, (because (0.3 / 0.1) # => 2.9999999999999996, not 3.0)
Related: Float#ceil
.
Returns self
(which is already a Float).
Returns the value as a rational.
2.0.to_r #=> (2/1) 2.5.to_r #=> (5/2) -0.75.to_r #=> (-3/4) 0.0.to_r #=> (0/1) 0.3.to_r #=> (5404319552844595/18014398509481984)
NOTE: 0.3.to_r isn’t the same as “0.3”.to_r. The latter is equivalent to “3/10”.to_r, but the former isn’t so.
0.3.to_r == 3/10r #=> false "0.3".to_r == 3/10r #=> true
See also Float#rationalize
.
Returns the current fiber. If you are not running in the context of a fiber this method will return the root fiber.
Resumes the fiber from the point at which the last Fiber.yield
was called, or starts running it if it is the first call to resume
. Arguments passed to resume will be the value of the Fiber.yield
expression or will be passed as block parameters to the fiber’s block if this is the first resume
.
Alternatively, when resume is called it evaluates to the arguments passed to the next Fiber.yield
statement inside the fiber’s block or to the block value if it runs to completion without any Fiber.yield
Returns an array of the entry names in the directory at dirpath
except for '.'
and '..'
; sets the given encoding onto each returned entry name:
Dir.children('/example') # => ["config.h", "lib", "main.rb"] Dir.children('/example').first.encoding # => #<Encoding:UTF-8> Dir.children('/example', encoding: 'US-ASCII').first.encoding # => #<Encoding:US-ASCII>
See String Encoding.
Raises an exception if the directory does not exist.
Reads and returns the next entry name from self
; returns nil
if at end-of-stream; see Dir As Stream-Like:
dir = Dir.new('example') dir.read # => "." dir.read # => ".." dir.read # => "config.h"
Returns an array of the entry names in self
except for '.'
and '..'
:
dir = Dir.new('/example') dir.children # => ["config.h", "lib", "main.rb"]
Sets the position in self
to zero; see Dir As Stream-Like:
dir = Dir.new('example') dir.read # => "." dir.read # => ".." dir.pos # => 2 dir.rewind # => #<Dir:example> dir.pos # => 0
Returns whether dirpath
is a directory in the underlying file system:
Dir.exist?('/example') # => true Dir.exist?('/nosuch') # => false Dir.exist?('/example/main.rb') # => false
Same as File.directory?
.
Returns a File::Stat
object for the file at filepath
(see File::Stat
):
File.stat('t.txt').class # => File::Stat
Like File::stat
, but does not follow the last symbolic link; instead, returns a File::Stat
object for the link itself.
File.symlink('t.txt', 'symlink') File.stat('symlink').size # => 47 File.lstat('symlink').size # => 5
Returns the name of the file referenced by the given link. Not available on all platforms.
File.symlink("testfile", "link2test") #=> 0 File.readlink("link2test") #=> "testfile"
Renames the given file to the new name. Raises a SystemCallError
if the file cannot be renamed.
File.rename("afile", "afile.bak") #=> 0
Returns the real (absolute) pathname of pathname in the actual filesystem not containing symlinks or useless dots.
If dir_string is given, it is used as a base directory for interpreting relative pathname instead of the current directory.
All components of the pathname must exist when this method is called.
Returns the real (absolute) pathname of pathname in the actual filesystem. The real pathname doesn’t contain symlinks or useless dots.
If dir_string is given, it is used as a base directory for interpreting relative pathname instead of the current directory.
The last component of the real pathname can be nonexistent.
Like File#stat
, but does not follow the last symbolic link; instead, returns a File::Stat
object for the link itself:
File.symlink('t.txt', 'symlink') f = File.new('symlink') f.stat.size # => 47 f.lstat.size # => 11
Return true
if the named file exists.
file_name can be an IO
object.
“file exists” means that stat() or fstat() system call is successful.