Returns a copy of the storage hash for the fiber. The method can only be called on the Fiber.current
.
Sets the storage hash for the fiber. This feature is experimental and may change in the future. The method can only be called on the Fiber.current
.
You should be careful about using this method as you may inadvertently clear important fiber-storage state. You should mostly prefer to assign specific keys in the storage using Fiber::[]=
.
You can also use Fiber.new(storage: nil)
to create a fiber with an empty storage.
Example:
while request = request_queue.pop # Reset the per-request state: Fiber.current.storage = nil handle_request(request) end
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
.
The fiber which receives the transfer call treats it much like a resume call. Arguments passed to transfer are treated like those passed to resume.
The two style of control passing to and from fiber (one is resume
and Fiber::yield
, another is transfer
to and from fiber) can’t be freely mixed.
If the Fiber’s lifecycle had started with transfer, it will never be able to yield or be resumed control passing, only finish or transfer back. (It still can resume other fibers that are allowed to be resumed.)
If the Fiber’s lifecycle had started with resume, it can yield or transfer to another Fiber
, but can receive control back only the way compatible with the way it was given away: if it had transferred, it only can be transferred back, and if it had yielded, it only can be resumed back. After that, it again can transfer or yield.
If those rules are broken FiberError
is raised.
For an individual Fiber
design, yield/resume is easier to use (the Fiber
just gives away control, it doesn’t need to think about who the control is given to), while transfer is more flexible for complex cases, allowing to build arbitrary graphs of Fibers dependent on each other.
Example:
manager = nil # For local var to be visible inside worker block # This fiber would be started with transfer # It can't yield, and can't be resumed worker = Fiber.new { |work| puts "Worker: starts" puts "Worker: Performed #{work.inspect}, transferring back" # Fiber.yield # this would raise FiberError: attempt to yield on a not resumed fiber # manager.resume # this would raise FiberError: attempt to resume a resumed fiber (double resume) manager.transfer(work.capitalize) } # This fiber would be started with resume # It can yield or transfer, and can be transferred # back or resumed manager = Fiber.new { puts "Manager: starts" puts "Manager: transferring 'something' to worker" result = worker.transfer('something') puts "Manager: worker returned #{result.inspect}" # worker.resume # this would raise FiberError: attempt to resume a transferring fiber Fiber.yield # this is OK, the fiber transferred from and to, now it can yield puts "Manager: finished" } puts "Starting the manager" manager.resume puts "Resuming the manager" # manager.transfer # this would raise FiberError: attempt to transfer to a yielding fiber manager.resume
produces
Starting the manager Manager: starts Manager: transferring 'something' to worker Worker: starts Worker: Performed "something", transferring back Manager: worker returned "Something" Resuming the manager Manager: finished
Returns false
if the current fiber is non-blocking. Fiber
is non-blocking if it was created via passing blocking: false
to Fiber.new
, or via Fiber.schedule
.
If the current Fiber
is blocking, the method returns 1. Future developments may allow for situations where larger integers could be returned.
Note that, even if the method returns false
, Fiber
behaves differently only if Fiber.scheduler
is set in the current thread.
See the “Non-blocking fibers” section in class docs for details.
Returns the Fiber
scheduler, that was last set for the current thread with Fiber.set_scheduler
. Returns nil
if no scheduler is set (which is the default), and non-blocking fibers’ behavior is the same as blocking. (see “Non-blocking fibers” section in class docs for details about the scheduler concept).
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 a pattern string or an Array
of pattern strings, and returns an array containing the matching filenames. If a block is given, calls the block once for each matching filename, passing the filename as a parameter to the block.
The optional base
keyword argument specifies the base directory for interpreting relative pathnames instead of the current working directory. As the results are not prefixed with the base directory name in this case, you will need to prepend the base directory name if you want real paths.
The results which matched single wildcard or character set are sorted in binary ascending order, unless false
is given as the optional sort
keyword argument. The order of an Array
of pattern strings and braces are preserved.
Note that the pattern is not a regexp, it’s closer to a shell glob. See File::fnmatch
for the meaning of the flags
parameter. Case sensitivity depends on your system (File::FNM_CASEFOLD
is ignored).
*
Matches any file. Can be restricted by other values in the glob. Equivalent to /.*/mx
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 if followed by /
. If this path segment contains any other characters, it is the same as the usual *
.
?
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"] Dir.glob(["*.rb", "*.h"]) #=> ["main.rb", "config.h"] Dir.glob("**/*.rb") #=> ["main.rb", # "lib/song.rb", # "lib/song/karaoke.rb"] Dir.glob("**/*.rb", base: "lib") #=> ["song.rb", # "song/karaoke.rb"] Dir.glob("**/lib") #=> ["lib"] Dir.glob("**/lib/**/*.rb") #=> ["lib/song.rb", # "lib/song/karaoke.rb"] Dir.glob("**/lib/*.rb") #=> ["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 true
if filepath
points to a block device, false
otherwise:
File.blockdev?('/dev/sda1') # => true File.blockdev?(File.new('t.tmp')) # => false
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
Returns the result of invoking exception.to_s
. Normally this returns the exception’s message or name.
Return the receiver associated with this KeyError
exception.
Return the receiver associated with this NameError
exception.
Return the receiver associated with this FrozenError
exception.
Return this SystemCallError’s error number.
Registers _filename_ to be loaded (using Kernel::require) the first time that _const_ (which may be a String or a symbol) is accessed in the namespace of _mod_. module A end A.autoload(:B, "b") A::B.doit # autoloads "b"
If const in mod is defined as autoload, the file name to be loaded is replaced with filename. If const is defined but not as autoload, does nothing.
Returns filename to be loaded if name is registered as autoload
in the namespace of mod or one of its ancestors.
module A end A.autoload(:B, "b") A.autoload?(:B) #=> "b"
If inherit
is false, the lookup only checks the autoloads in the receiver:
class A autoload :CONST, "const.rb" end class B < A end B.autoload?(:CONST) #=> "const.rb", found in A (ancestor) B.autoload?(:CONST, false) #=> nil, not found in B itself
Internal method used to provide marshalling support. See the Marshal
module.
Returns the remainder from dividing by the value.
x.remainder(y) means x-y*(x/y).truncate
Return the largest integer less than or equal to the value, as a BigDecimal
.
BigDecimal('3.14159').floor #=> 3 BigDecimal('-9.1').floor #=> -10
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').floor(3) #=> 3.141 BigDecimal('13345.234').floor(-2) #=> 13300.0
Returns the value raised to the power of n.
Note that n must be an Integer
.
Also available as the operator **.