Returns true
if num
has a zero value.
Returns self
if num
is not zero, nil
otherwise.
This behavior is useful when chaining comparisons:
a = %w( z Bb bB bb BB a aA Aa AA A ) b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b } b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
Returns the largest integer less than or equal to num
.
Numeric
implements this by converting an Integer
to a Float
and invoking Float#floor
.
1.floor #=> 1 (-1).floor #=> -1
Returns the numerator.
Convert self
to locale encoding
Inserts other_str before the character at the given index, modifying str. Negative indices count from the end of the string, and insert after the given character. The intent is insert aString so that it starts at the given index.
"abcd".insert(0, 'X') #=> "Xabcd" "abcd".insert(3, 'X') #=> "abcXd" "abcd".insert(4, 'X') #=> "abcdX" "abcd".insert(-3, 'X') #=> "abXcd" "abcd".insert(-1, 'X') #=> "abcdX"
returns the indexth byte as an integer.
Returns a new string with the characters from str in reverse order.
"stressed".reverse #=> "desserts"
Reverses str in place.
Returns the Symbol
corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name
.
"Koala".intern #=> :Koala s = 'cat'.to_sym #=> :cat s == :cat #=> true s = '@cat'.to_sym #=> :@cat s == :@cat #=> true
This can also be used to create symbols that cannot be represented using the :xxx
notation.
'cat and dog'.to_sym #=> :"cat and dog"
Centers str
in width
. If width
is greater than the length of str
, returns a new String of length width
with str
centered and padded with padstr
; otherwise, returns str
.
"hello".center(4) #=> "hello" "hello".center(20) #=> " hello " "hello".center(20, '123') #=> "1231231hello12312312"
provides a unified clone
operation, for REXML::XPathParser
to use across multiple Object
types
Returns an array with both a numeric
and a float
represented as Float
objects.
This is achieved by converting a numeric
to a Float
.
1.2.coerce(3) #=> [3.0, 1.2] 2.5.coerce(1.1) #=> [1.1, 2.5]
Return the modulo after division of float
by other
.
6543.21.modulo(137) #=> 104.21 6543.21.modulo(137.24) #=> 92.9299999999996
Returns true
if float
is 0.0.
Returns the largest integer less than or equal to float
.
1.2.floor #=> 1 2.0.floor #=> 2 (-1.2).floor #=> -2 (-2.0).floor #=> -2
Returns the numerator. The result is machine dependent.
n = 0.3.numerator #=> 5404319552844595 d = 0.3.denominator #=> 18014398509481984 n.fdiv(d) #=> 0.3
Transfer control to another fiber, resuming it from where it last stopped or starting it if it was not resumed before. The calling fiber will be suspended much like in a call to Fiber.yield
. You need to require 'fiber'
before using this method.
The fiber which receives the transfer call is treats it much like a resume call. Arguments passed to transfer are treated like those passed to resume.
You cannot resume a fiber that transferred control to another one. This will cause a double resume error. You need to transfer control back to this fiber before it can yield and resume.
Example:
fiber1 = Fiber.new do puts "In Fiber 1" Fiber.yield end fiber2 = Fiber.new do puts "In Fiber 2" fiber1.transfer puts "Never see this message" end fiber3 = Fiber.new do puts "In Fiber 3" end fiber2.resume fiber3.resume
produces
In fiber 2 In fiber 1 In fiber 3
Closes the directory stream. Calling this method on closed Dir
object is ignored since Ruby 2.3.
d = Dir.new("testdir") d.close #=> nil
Returns the path to the current working directory of this process as a string.
Dir.chdir("/tmp") #=> 0 Dir.getwd #=> "/tmp" Dir.pwd #=> "/tmp"
Expands pattern
, which is an Array of patterns or a pattern String, and returns the results as matches
or as arguments given to the block.
Note that this pattern is not a regexp, it’s closer to a shell glob. See File::fnmatch
for the meaning of the flags
parameter. Note that case sensitivity depends on your system (so File::FNM_CASEFOLD is ignored), as does the order in which the results are returned.
*
Matches any file. Can be restricted by other values in the glob. Equivalent to / .* /x
in regexp.
*
Matches all files
c*
Matches all files beginning with c
*c
Matches all files ending with c
*c*
Match all files that have c
in them (including at the beginning or end).
Note, this will not match Unix-like hidden files (dotfiles). In order to include those in the match results, you must use the File::FNM_DOTMATCH flag or something like "{*,.*}"
.
**
Matches directories recursively.
?
Matches any one character. Equivalent to /.{1}/
in regexp.
[set]
Matches any one character in set
. Behaves exactly like character sets in Regexp
, including set negation ([^a-z]
).
{p,q}
Matches either literal p
or literal q
. Equivalent to pattern alternation in regexp.
Matching literals may be more than one character in length. More than two literals may be specified.
\
Escapes the next metacharacter.
Note that this means you cannot use backslash on windows as part of a glob, i.e. Dir["c:\foo*"]
will not work, use Dir["c:/foo*"]
instead.
Examples:
Dir["config.?"] #=> ["config.h"] Dir.glob("config.?") #=> ["config.h"] Dir.glob("*.[a-z][a-z]") #=> ["main.rb"] Dir.glob("*.[^r]*") #=> ["config.h"] Dir.glob("*.{rb,h}") #=> ["main.rb", "config.h"] Dir.glob("*") #=> ["config.h", "main.rb"] Dir.glob("*", File::FNM_DOTMATCH) #=> [".", "..", "config.h", "main.rb"] rbfiles = File.join("**", "*.rb") Dir.glob(rbfiles) #=> ["main.rb", # "lib/song.rb", # "lib/song/karaoke.rb"] libdirs = File.join("**", "lib") Dir.glob(libdirs) #=> ["lib"] librbfiles = File.join("**", "lib", "**", "*.rb") Dir.glob(librbfiles) #=> ["lib/song.rb", # "lib/song/karaoke.rb"] librbfiles = File.join("**", "lib", "*.rb") Dir.glob(librbfiles) #=> ["lib/song.rb"]
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.
Returns a Digest
subclass by name
in a thread-safe manner even when on-demand loading is involved.
require 'digest' Digest("MD5") # => Digest::MD5 Digest(:SHA256) # => Digest::SHA256 Digest(:Foo) # => LoadError: library not found for class Digest::Foo -- digest/foo