Generated when trying to lookup a gem to indicate that the gem was found, but that it isn’t usable on the current platform.
fetch and install read these and report them to the user to aid in figuring out why a gem couldn’t be installed.
Used to raise parsing and loading errors
The UriFormatter
handles URIs from user-input and escaping.
uf = Gem::UriFormatter.new 'example.com' p uf.normalize #=> 'http://example.com'
Explains syntax errors based on their source
example:
source = "def foo; puts 'lol'" # Note missing end explain ExplainSyntax.new( code_lines: CodeLine.from_source(source) ).call explain.errors.first # => "Unmatched keyword, missing `end' ?"
When the error cannot be determined by lexical counting then the parser is run against the input and the raw errors are returned.
Example:
source = "1 * " # Note missing a second number explain ExplainSyntax.new( code_lines: CodeLine.from_source(source) ).call explain.errors.first # => "syntax error, unexpected end-of-input"
RFC6068, the mailto URL scheme.
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"
An ObjectSpace::WeakMap
is a key-value map that holds weak references to its keys and values, so they can be garbage-collected when there are no more references left.
Keys in the map are compared by identity.
m = ObjectSpace::WeakMap.new key1 = "foo" val1 = Object.new m[key1] = val1 key2 = "foo" val2 = Object.new m[key2] = val2 m[key1] #=> #<Object:0x0...> m[key2] #=> #<Object:0x0...> val1 = nil # remove the other reference to value GC.start m[key1] #=> nil m.keys #=> ["bar"] key2 = nil # remove the other reference to key GC.start m[key2] #=> nil m.keys #=> []
(Note that GC.start
is used here only for demonstrational purposes and might not always lead to demonstrated results.)
See also ObjectSpace::WeakKeyMap
map class, which compares keys by value, and holds weak references only to the keys.
An ObjectSpace::WeakKeyMap
is a key-value map that holds weak references to its keys, so they can be garbage collected when there is no more references.
Unlike ObjectSpace::WeakMap
:
references to values are strong, so they aren’t garbage collected while they are in the map;
keys are compared by value (using Object#eql?
), not by identity;
only garbage-collectable objects can be used as keys.
map = ObjectSpace::WeakKeyMap.new val = Time.new(2023, 12, 7) key = "name" map[key] = val # Value is fetched by equality: the instance of string "name" is # different here, but it is equal to the key map["name"] #=> 2023-12-07 00:00:00 +0200 val = nil GC.start # There are no more references to `val`, yet the pair isn't # garbage-collected. map["name"] #=> 2023-12-07 00:00:00 +0200 key = nil GC.start # There are no more references to `key`, key and value are # garbage-collected. map["name"] #=> nil
(Note that GC.start
is used here only for demonstrational purposes and might not always lead to demonstrated results.)
The collection is especially useful for implementing caches of lightweight value objects, so that only one copy of each value representation would be stored in memory, but the copies that aren’t used would be garbage-collected.
CACHE = ObjectSpace::WeakKeyMap def make_value(**) val = ValueObject.new(**) if (existing = @cache.getkey(val)) # if the object with this value exists, we return it existing else # otherwise, put it in the cache @cache[val] = true val end end
This will result in make_value
returning the same object for same set of attributes always, but the values that aren’t needed anymore woudn’t be sitting in the cache forever.
AbstractSyntaxTree
provides methods to parse Ruby code into abstract syntax trees. The nodes in the tree are instances of RubyVM::AbstractSyntaxTree::Node
.
This module is MRI specific as it exposes implementation details of the MRI abstract syntax tree.
This module is experimental and its API is not stable, therefore it might change without notice. As examples, the order of children nodes is not guaranteed, the number of children nodes might change, there is no way to access children nodes by name, etc.
If you are looking for a stable API or an API working under multiple Ruby implementations, consider using the parser gem or Ripper
. If you would like to make RubyVM::AbstractSyntaxTree
stable, please join the discussion at bugs.ruby-lang.org/issues/14844.
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
Commands will be placed in this namespace
This module is used for safely loading Marshal
specs from a gem. The ‘safe_load` method defined on this module is specifically designed for loading Gem specifications.
This class represents a YAML Mapping.
A Psych::Nodes::Mapping
node may have 0 or more children, but must have an even number of children. Here are the valid children a Psych::Nodes::Mapping
node may have:
Raised if the mask given to a binary operation is invalid, e.g. zero length or overlaps the target buffer.
The result of parsing a pack template.