The Singleton
module implements the Singleton
pattern.
To use Singleton
, include the module in your class.
class Klass include Singleton # ... end
This ensures that only one instance of Klass can be created.
a,b = Klass.instance, Klass.instance a == b # => true Klass.new # => NoMethodError - new is private ...
The instance is created at upon the first call of Klass.instance().
class OtherKlass include Singleton # ... end ObjectSpace.each_object(OtherKlass){} # => 0 OtherKlass.instance ObjectSpace.each_object(OtherKlass){} # => 1
This behavior is preserved under inheritance and cloning.
This above is achieved by:
Making Klass.new and Klass.allocate private.
Overriding Klass.inherited(sub_klass) and Klass.clone() to ensure that the Singleton
properties are kept when inherited and cloned.
Providing the Klass.instance() method that returns the same object each time it is called.
Overriding Klass._load(str) to call Klass.instance().
Overriding Klass#clone and Klass#dup to raise TypeErrors to prevent cloning or duping.
Singleton
and Marshal
By default Singleton’s _dump(depth)
returns the empty string. Marshalling by default will strip state information, e.g. instance variables from the instance. Classes using Singleton
can provide custom _load(str) and _dump(depth) methods to retain some of the previous state of the instance.
require 'singleton' class Example include Singleton attr_accessor :keep, :strip def _dump(depth) # this strips the @strip information from the instance Marshal.dump(@keep, depth) end def self._load(str) instance.keep = Marshal.load(str) instance end end a = Example.instance a.keep = "keep this" a.strip = "get rid of this" stored_state = Marshal.dump(a) a.keep = nil a.strip = nil b = Marshal.load(stored_state) p a == b # => true p a.keep # => "keep this" p a.strip # => nil
define UnicodeNormalize module here so that we don’t have to look it up
Enumerator::Product
generates a Cartesian product of any number of enumerable objects. Iterating over the product of enumerable objects is roughly equivalent to nested each_entry loops where the loop for the rightmost object is put innermost.
innings = Enumerator::Product.new(1..9, ['top', 'bottom']) innings.each do |i, h| p [i, h] end # [1, "top"] # [1, "bottom"] # [2, "top"] # [2, "bottom"] # [3, "top"] # [3, "bottom"] # ... # [9, "top"] # [9, "bottom"]
The method used against each enumerable object is ‘each_entry` instead of `each` so that the product of N enumerable objects yields an array of exactly N elements in each iteration.
When no enumerator is given, it calls a given block once yielding an empty argument list.
This type of objects can be created by Enumerator.product
.
Response class for Multiple Choices
responses (status code 300).
The Multiple Choices
response indicates that the server offers multiple options for the resource from which the client may choose. See 300 Multiple Choices.
Response class for Multiple Choices
responses (status code 300).
The Multiple Choices
response indicates that the server offers multiple options for the resource from which the client may choose. See 300 Multiple Choices.
Find
mis-matched syntax based on lexical count
Used for detecting missing pairs of elements each keyword needs an end, each ‘{’ needs a ‘}’ etc.
Example:
left_right = LeftRightLexCount.new left_right.count_kw left_right.missing.first # => "end" left_right = LeftRightLexCount.new source = "{ a: b, c: d" # Note missing '}' LexAll.new(source: source).each do |lex| left_right.count_lex(lex) end left_right.missing.first # => "}"
Response class for Unprocessable Entity
responses (status code 422).
The request was well-formed but had semantic errors. See 422 Unprocessable Entity.
Signals that a file permission error is preventing the user from operating on the given directory.
There are three main phases in the algorithm:
Sanitize/format input source
Search for invalid blocks
Format invalid blocks into something meaninful
The Code frontier is a critical part of the second step
## Knowing where we’ve been
Once a code block is generated it is added onto the frontier. Then it will be sorted by indentation and frontier can be filtered. Large blocks that fully enclose a smaller block will cause the smaller block to be evicted.
CodeFrontier#<<(block) # Adds block to frontier CodeFrontier#pop # Removes block from frontier
## Knowing where we can go
Internally the frontier keeps track of “unvisited” lines which are exposed via ‘next_indent_line` when called, this method returns, a line of code with the highest indentation.
The returned line of code can be used to build a CodeBlock
and then that code block is added back to the frontier. Then, the lines are removed from the “unvisited” so we don’t double-create the same block.
CodeFrontier#next_indent_line # Shows next line CodeFrontier#register_indent_block(block) # Removes lines from unvisited
## Knowing when to stop
The frontier knows how to check the entire document for a syntax error. When blocks are added onto the frontier, they’re removed from the document. When all code containing syntax errors has been added to the frontier, the document will be parsable without a syntax error and the search can stop.
CodeFrontier#holds_all_syntax_errors? # Returns true when frontier holds all syntax errors
## Filtering false positives
Once the search is completed, the frontier may have multiple blocks that do not contain the syntax error. To limit the result to the smallest subset of “invalid blocks” call:
CodeFrontier#detect_invalid_blocks
Mixin module providing HTML generation methods.
For example,
cgi.a("http://www.example.com") { "Example" } # => "<A HREF=\"http://www.example.com\">Example</A>"
Modules Html3, Html4, etc., contain more basic HTML-generation methods (#title
, #h1
, etc.).
See class CGI
for a detailed example.
Configuration options for dumping YAML
.
An InstalledSpecification
represents a gem that is already installed locally.
Cleared reference exception
This exception is raised if the required unicode support is missing on the system. Usually this means that the iconv library is not installed.
Exception
raised when there is an invalid encoding detected
Response class for Multi-Status (WebDAV)
responses (status code 207).
The Multi-Status (WebDAV)
response indicates that the server has received the request, and that the message body can contain a number of separate response codes. See 207 Multi-Status (WebDAV).
Response class for Range Not Satisfiable
responses (status code 416).
The request entity has a media type which the server or resource does not support. See 416 Range Not Satisfiable.
Response class for Range Not Satisfiable
responses (status code 416).
The request entity has a media type which the server or resource does not support. See 416 Range Not Satisfiable.
Response class for Failed Dependency (WebDAV)
responses (status code 424).
The request failed because it depended on another request and that request failed. See 424 Failed Dependency (WebDAV).
Response class for Not Implemented
responses (status code 501).
The server either does not recognize the request method, or it lacks the ability to fulfil the request. See 501 Not Implemented.
Response class for Service Unavailable
responses (status code 503).
The server cannot handle the request (because it is overloaded or down for maintenance). See 503 Service Unavailable.
Hash
with completion search feature. See OptionParser::Completion
.