URI
is valid, bad usage is not.
RefError
is raised when a referenced object has been recycled by the garbage collector
Raised when a mathematical function is evaluated outside of its domain of definition.
For example, since cos
returns values in the range -1..1, its inverse function acos
is only defined on that interval:
Math.acos(42)
produces:
Math::DomainError: Numerical argument is out of domain - "acos"
Raised on attempt to Ractor#take
if there was an uncaught exception in the Ractor
. Its cause
will contain the original exception, and ractor
is the original ractor it was raised in.
r = Ractor.new { raise "Something weird happened" } begin r.take rescue => e p e # => #<Ractor::RemoteError: thrown by remote Ractor.> p e.ractor == r # => true p e.cause # => #<RuntimeError: Something weird happened> end
Raised on an attempt to access an object which was moved in Ractor#send
or Ractor.yield
.
r = Ractor.new { sleep } ary = [1, 2, 3] r.send(ary, move: true) ary.inspect # Ractor::MovedError (can not send any methods to a moved object)
This is not an existing class, but documentation of the interface that Scheduler
object should comply to in order to be used as argument to Fiber.scheduler
and handle non-blocking fibers. See also the “Non-blocking fibers” section in Fiber
class docs for explanations of some concepts.
Scheduler’s behavior and usage are expected to be as follows:
When the execution in the non-blocking Fiber
reaches some blocking operation (like sleep, wait for a process, or a non-ready I/O), it calls some of the scheduler’s hook methods, listed below.
Scheduler
somehow registers what the current fiber is waiting on, and yields control to other fibers with Fiber.yield
(so the fiber would be suspended while expecting its wait to end, and other fibers in the same thread can perform)
At the end of the current thread execution, the scheduler’s method scheduler_close is called
The scheduler runs into a wait loop, checking all the blocked fibers (which it has registered on hook calls) and resuming them when the awaited resource is ready (e.g. I/O ready or sleep time elapsed).
This way concurrent execution will be achieved transparently for every individual Fiber’s code.
Scheduler
implementations are provided by gems, like Async.
Hook methods are:
io_wait
, io_read
, io_write
, io_pread
, io_pwrite
, and io_select
, io_close
(the list is expanded as Ruby developers make more methods having non-blocking calls)
When not specified otherwise, the hook implementations are mandatory: if they are not implemented, the methods trying to call hook will fail. To provide backward compatibility, in the future hooks will be optional (if they are not implemented, due to the scheduler being created for the older Ruby version, the code which needs this hook will not fail, and will just behave in a blocking fashion).
It is also strongly recommended that the scheduler implements the fiber
method, which is delegated to by Fiber.schedule
.
Sample toy implementation of the scheduler can be found in Ruby’s code, in test/fiber/scheduler.rb
Raised by Encoding
and String
methods when the string being transcoded contains a byte invalid for the either the source or target encoding.
A mixin that provides methods for parsing C struct and prototype signatures.
require 'fiddle/import' include Fiddle::CParser #=> Object parse_ctype('int') #=> Fiddle::TYPE_INT parse_struct_signature(['int i', 'char c']) #=> [[Fiddle::TYPE_INT, Fiddle::TYPE_CHAR], ["i", "c"]] parse_signature('double sum(double, double)') #=> ["sum", Fiddle::TYPE_DOUBLE, [Fiddle::TYPE_DOUBLE, Fiddle::TYPE_DOUBLE]]
A DSL that provides the means to dynamically load libraries and build modules around them including calling extern functions within the C library that has been loaded.
require 'fiddle' require 'fiddle/import' module LibSum extend Fiddle::Importer dlload './libsum.so' extern 'double sum(double*, int)' extern 'double split(double)' end
Used to construct C classes (CUnion
, CStruct
, etc)
Fiddle::Importer#struct
and Fiddle::Importer#union
wrap this functionality in an easy-to-use manner.
OpenSSL
IO
buffering mix-in module.
This module allows an OpenSSL::SSL::SSLSocket
to behave like an IO
.
You typically won’t use this module directly, you can see it implemented in OpenSSL::SSL::SSLSocket
.
Mixin module that provides the following:
Access to the CGI
environment variables as methods. See documentation to the CGI
class for a list of these variables. The methods are exposed by removing the leading HTTP_
(if it exists) and downcasing the name. For example, auth_type
will return the environment variable AUTH_TYPE
, and accept
will return the value for HTTP_ACCEPT
.
Access to cookies, including the cookies attribute.
Access to parameters, including the params attribute, and overloading []
to perform parameter value lookup by key.
The initialize_query
method, for initializing the above mechanisms, handling multipart forms, and allowing the class to be used in “offline” mode.
This module has all methods of FileUtils
module, but it outputs messages before acting. This equates to passing the :verbose
flag to methods in FileUtils
.
The HTTPHeader module provides access to HTTP headers.
The module is included in:
Net::HTTPGenericRequest
(and therefore Net::HTTPRequest
).
The headers are a hash-like collection of key/value pairs called fields.
Headers may be included in:
A Net::HTTPRequest
object: the object’s headers will be sent with the request. Any fields may be defined in the request; see Setters.
A Net::HTTPResponse
object: the objects headers are usually those returned from the host. Fields may be retrieved from the object; see Getters and Iterators.
Exactly which fields should be sent or expected depends on the host; see:
A header field is a key/value pair.
A field key may be:
A string: Key 'Accept'
is treated as if it were 'Accept'.downcase
; i.e., 'accept'
.
A symbol: Key :Accept
is treated as if it were :Accept.to_s.downcase
; i.e., 'accept'
.
Examples:
req = Net::HTTP::Get.new(uri) req[:accept] # => "*/*" req['Accept'] # => "*/*" req['ACCEPT'] # => "*/*" req['accept'] = 'text/html' req[:accept] = 'text/html' req['ACCEPT'] = 'text/html'
A field value may be returned as an array of strings or as a string:
These methods return field values as arrays:
get_fields
: Returns the array value for the given key, or nil
if it does not exist.
to_hash
: Returns a hash of all header fields: each key is a field name; its value is the array value for the field.
These methods return field values as string; the string value for a field is equivalent to self[key.downcase.to_s].join(', '))
:
The field value may be set:
[]=
: Sets the value for the given key; the given value may be a string, a symbol, an array, or a hash.
add_field
: Adds a given value to a value for the given key (not overwriting the existing value).
delete
: Deletes the field for the given key.
Example field values:
String:
req['Accept'] = 'text/html' # => "text/html" req['Accept'] # => "text/html" req.get_fields('Accept') # => ["text/html"]
Symbol:
req['Accept'] = :text # => :text req['Accept'] # => "text" req.get_fields('Accept') # => ["text"]
Simple array:
req[:foo] = %w[bar baz bat] req[:foo] # => "bar, baz, bat" req.get_fields(:foo) # => ["bar", "baz", "bat"]
Simple hash:
req[:foo] = {bar: 0, baz: 1, bat: 2} req[:foo] # => "bar, 0, baz, 1, bat, 2" req.get_fields(:foo) # => ["bar", "0", "baz", "1", "bat", "2"]
Nested:
req[:foo] = [%w[bar baz], {bat: 0, bam: 1}] req[:foo] # => "bar, baz, bat, 0, bam, 1" req.get_fields(:foo) # => ["bar", "baz", "bat", "0", "bam", "1"] req[:foo] = {bar: %w[baz bat], bam: {bah: 0, bad: 1}} req[:foo] # => "bar, baz, bat, bam, bah, 0, bad, 1" req.get_fields(:foo) # => ["bar", "baz", "bat", "bam", "bah", "0", "bad", "1"]
Various convenience methods retrieve values, set values, query values, set form values, or iterate over fields.
Method []=
can set any field, but does little to validate the new value; some of the other setter methods provide some validation:
[]=
: Sets the string or array value for the given key.
add_field
: Creates or adds to the array value for the given key.
basic_auth
: Sets the string authorization header for 'Authorization'
.
content_length=
: Sets the integer length for field 'Content-Length
.
content_type=
: Sets the string value for field 'Content-Type'
.
proxy_basic_auth
: Sets the string authorization header for 'Proxy-Authorization'
.
set_range
: Sets the value for field 'Range'
.
set_form
: Sets an HTML form data set.
set_form_data
: Sets header fields and a body from HTML form data.
Method []
can retrieve the value of any field that exists, but always as a string; some of the other getter methods return something different from the simple string value:
[]
: Returns the string field value for the given key.
content_length
: Returns the integer value of field 'Content-Length'
.
content_range
: Returns the Range
value of field 'Content-Range'
.
content_type
: Returns the string value of field 'Content-Type'
.
fetch
: Returns the string field value for the given key.
get_fields
: Returns the array field value for the given key
.
main_type
: Returns first part of the string value of field 'Content-Type'
.
sub_type
: Returns second part of the string value of field 'Content-Type'
.
range
: Returns an array of Range
objects of field 'Range'
, or nil
.
range_length
: Returns the integer length of the range given in field 'Content-Range'
.
type_params
: Returns the string parameters for 'Content-Type'
.
chunked?
: Returns whether field 'Transfer-Encoding'
is set to 'chunked'
.
connection_close?
: Returns whether field 'Connection'
is set to 'close'
.
connection_keep_alive?
: Returns whether field 'Connection'
is set to 'keep-alive'
.
key?
: Returns whether a given key exists.
each_capitalized
: Passes each field capitalized-name/value pair to the block.
each_capitalized_name
: Passes each capitalized field name to the block.
each_header
: Passes each field name/value pair to the block.
each_name
: Passes each field name to the block.
each_value
: Passes each string field value to the block.
Formats generated random numbers in many manners. When 'random/formatter'
is required, several methods are added to empty core module Random::Formatter
, making them available as Random’s instance and module methods.
Standard library SecureRandom
is also extended with the module, and the methods described below are available as a module methods in it.
Generate random hexadecimal strings:
require 'random/formatter' prng = Random.new prng.hex(10) #=> "52750b30ffbc7de3b362" prng.hex(10) #=> "92b15d6c8dc4beb5f559" prng.hex(13) #=> "39b290146bea6ce975c37cfc23" # or just Random.hex #=> "1aed0c631e41be7f77365415541052ee"
Generate random base64 strings:
prng.base64(10) #=> "EcmTPZwWRAozdA==" prng.base64(10) #=> "KO1nIU+p9DKxGg==" prng.base64(12) #=> "7kJSM/MzBJI+75j8" Random.base64(4) #=> "bsQ3fQ=="
Generate random binary strings:
prng.random_bytes(10) #=> "\016\t{\370g\310pbr\301" prng.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337" Random.random_bytes(6) #=> "\xA1\xE6Lr\xC43"
Generate alphanumeric strings:
prng.alphanumeric(10) #=> "S8baxMJnPl" prng.alphanumeric(10) #=> "aOxAg8BAJe" Random.alphanumeric #=> "TmP9OsJHJLtaZYhP"
Generate UUIDs:
prng.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594" prng.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab" Random.uuid #=> "f14e0271-de96-45cc-8911-8910292a42cd"
All methods are available in the standard library SecureRandom
, too:
SecureRandom.hex #=> "05b45376a30c67238eb93b16499e50cf"
Generate a random number in the given range as Random
does
prng.random_number #=> 0.5816771641321361 prng.random_number(1000) #=> 485 prng.random_number(1..6) #=> 3 prng.rand #=> 0.5816771641321361 prng.rand(1000) #=> 485 prng.rand(1..6) #=> 3
Mixin methods for local and remote Gem::Command
options.