Modifies str in place as described for String#chomp
, returning str, or nil
if no modifications were made.
Returns the absolute value of float
.
(-34.56).abs #=> 34.56 -34.56.abs #=> 34.56 34.56.abs #=> 34.56
Float#magnitude
is an alias for Float#abs
.
Returns float
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 floating point number when ndigits
is positive, otherwise returns an integer.
2.8.truncate #=> 2 (-2.8).truncate #=> -2 1.234567.truncate(2) #=> 1.23 34567.89.truncate(-2) #=> 34500
Note that the limited precision of floating point arithmetic might lead to surprising results:
(0.3 / 0.1).truncate #=> 2 (!)
Returns true
if float
is less than 0.
Returns the numerator. The result is machine dependent.
n = 0.3.numerator #=> 5404319552844595 d = 0.3.denominator #=> 18014398509481984 n.fdiv(d) #=> 0.3
See also Float#denominator
.
Returns the denominator (always positive). The result is machine dependent.
See also Float#numerator
.
Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). If the optional argument eps
is not given, it will be chosen automatically.
0.3.rationalize #=> (3/10) 1.333.rationalize #=> (1333/1000) 1.333.rationalize(0.01) #=> (4/3)
See also Float#to_r
.
Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.
If no block is given, an enumerator is returned instead.
Dir.foreach("testdir") {|x| puts "Got #{x}" }
produces:
Got . Got .. Got config.h Got main.rb
Returns an array containing all of the filenames except for “.” and “..” in the given directory. Will raise a SystemCallError
if the named directory doesn’t exist.
The optional encoding keyword argument specifies the encoding of the directory. If not specified, the filesystem encoding is used.
Dir.children("testdir") #=> ["config.h", "main.rb"]
Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.
If no block is given, an enumerator is returned instead.
d = Dir.new("testdir") d.each {|x| puts "Got #{x}" }
produces:
Got . Got .. Got config.h Got main.rb
Returns an array containing all of the filenames except for “.” and “..” in this directory.
d = Dir.new("testdir") d.children #=> ["config.h", "main.rb"]
Changes the current working directory of the process to the given string. When called without an argument, changes the directory to the value of the environment variable HOME
, or LOGDIR
. SystemCallError
(probably Errno::ENOENT) if the target directory does not exist.
If a block is given, it is passed the name of the new current directory, and the block is executed with that as the current directory. The original working directory is restored when the block exits. The return value of chdir
is the value of the block. chdir
blocks can be nested, but in a multi-threaded program an error will be raised if a thread attempts to open a chdir
block while another thread has one open.
Dir.chdir("/var/spool/mail") puts Dir.pwd Dir.chdir("/tmp") do puts Dir.pwd Dir.chdir("/usr") do puts Dir.pwd end puts Dir.pwd end puts Dir.pwd
produces:
/var/spool/mail /tmp /usr /tmp /var/spool/mail
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.
Returns a File::Stat
object for the named file (see File::Stat
).
File.stat("testfile").mtime #=> Tue Apr 08 12:58:04 CDT 2003
Same as File::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 File.lstat("link2test").size #=> 8 File.stat("link2test").size #=> 66
Returns the last access time for the named file as a Time
object.
file_name can be an IO
object.
File.atime("testfile") #=> Wed Apr 09 08:51:48 CDT 2003
Changes permission bits on the named file(s) to the bit pattern represented by mode_int. Actual effects are operating system dependent (see the beginning of this section). On Unix systems, see chmod(2)
for details. Returns the number of files processed.
File.chmod(0644, "testfile", "out") #=> 2
Changes the owner and group of the named file(s) 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. Returns the number of files processed.
File.chown(nil, 100, "testfile")
Equivalent to File::chmod
, but does not follow symbolic links (so it will change the permissions associated with the link, not the file referenced by the link). Often not available.
Equivalent to File::chown
, but does not follow symbolic links (so it will change the owner associated with the link, not the file referenced by the link). Often not available. Returns number of files in the argument list.
Returns the current umask value for this process. If the optional argument is given, set the umask to that value and return the previous value. Umask values are subtracted from the default permissions, so a umask of 0222
would make a file read-only for everyone.
File.umask(0006) #=> 18 File.umask #=> 6
Truncates the file file_name to be at most integer bytes long. Not available on all platforms.
f = File.new("out", "w") f.write("1234567890") #=> 10 f.close #=> nil File.truncate("out", 5) #=> 0 File.size("out") #=> 5
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.