This module manipulates strings according to the word parsing rules of the UNIX Bourne shell.
The shellwords() function was originally a port of shellwords.pl, but modified to conform to the Shell
& Utilities volume of the IEEE Std 1003.1-2008, 2016 Edition [1].
You can use Shellwords
to parse a string into a Bourne shell friendly Array.
require 'shellwords' argv = Shellwords.split('three blind "mice"') argv #=> ["three", "blind", "mice"]
Once you’ve required Shellwords
, you can use the split alias String#shellsplit
.
argv = "see how they run".shellsplit argv #=> ["see", "how", "they", "run"]
Be careful you don’t leave a quote unmatched.
argv = "they all ran after the farmer's wife".shellsplit #=> ArgumentError: Unmatched double quote: ...
In this case, you might want to use Shellwords.escape
, or its alias String#shellescape
.
This method will escape the String for you to safely use with a Bourne shell.
argv = Shellwords.escape("special's.txt") argv #=> "special\\'s.txt" system("cat " + argv)
Shellwords
also comes with a core extension for Array, Array#shelljoin
.
argv = %w{ls -lta lib} system(argv.shelljoin)
You can use this method to create an escaped string out of an array of tokens separated by a space. In this example we used the literal shortcut for Array.new
.
Wakou Aoyama
Akinori MUSHA <knu@iDaemons.org>
Akinori MUSHA <knu@iDaemons.org> (current maintainer)
1: IEEE Std 1003.1-2008, 2016 Edition, the Shell & Utilities volume
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 and taint state, 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" a.taint 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
TSort
implements topological sorting using Tarjan’s algorithm for strongly connected components.
TSort
is designed to be able to be used with any object which can be interpreted as a directed graph.
TSort
requires two methods to interpret an object as a graph, tsort_each_node
and tsort_each_child.
tsort_each_node
is used to iterate for all nodes over a graph.
tsort_each_child
is used to iterate for child nodes of a given node.
The equality of nodes are defined by eql? and hash since TSort
uses Hash
internally.
The following example demonstrates how to mix the TSort
module into an existing class (in this case, Hash
). Here, we’re treating each key in the hash as a node in the graph, and so we simply alias the required tsort_each_node
method to Hash’s each_key method. For each key in the hash, the associated value is an array of the node’s child nodes. This choice in turn leads to our implementation of the required tsort_each_child
method, which fetches the array of child nodes and then iterates over that array using the user-supplied block.
require 'tsort' class Hash include TSort alias tsort_each_node each_key def tsort_each_child(node, &block) fetch(node).each(&block) end end {1=>[2, 3], 2=>[3], 3=>[], 4=>[]}.tsort #=> [3, 2, 1, 4] {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}.strongly_connected_components #=> [[4], [2, 3], [1]]
A very simple ‘make’ like tool can be implemented as follows:
require 'tsort' class Make def initialize @dep = {} @dep.default = [] end def rule(outputs, inputs=[], &block) triple = [outputs, inputs, block] outputs.each {|f| @dep[f] = [triple]} @dep[triple] = inputs end def build(target) each_strongly_connected_component_from(target) {|ns| if ns.length != 1 fs = ns.delete_if {|n| Array === n} raise TSort::Cyclic.new("cyclic dependencies: #{fs.join ', '}") end n = ns.first if Array === n outputs, inputs, block = n inputs_time = inputs.map {|f| File.mtime f}.max begin outputs_time = outputs.map {|f| File.mtime f}.min rescue Errno::ENOENT outputs_time = nil end if outputs_time == nil || inputs_time != nil && outputs_time <= inputs_time sleep 1 if inputs_time != nil && inputs_time.to_i == Time.now.to_i block.call end end } end def tsort_each_child(node, &block) @dep[node].each(&block) end include TSort end def command(arg) print arg, "\n" system arg end m = Make.new m.rule(%w[t1]) { command 'date > t1' } m.rule(%w[t2]) { command 'date > t2' } m.rule(%w[t3]) { command 'date > t3' } m.rule(%w[t4], %w[t1 t3]) { command 'cat t1 t3 > t4' } m.rule(%w[t5], %w[t4 t2]) { command 'cat t4 t2 > t5' } m.build('t5')
‘tsort.rb’ is wrong name because this library uses Tarjan’s algorithm for strongly connected components. Although ‘strongly_connected_components.rb’ is correct but too long.
Tarjan, “Depth First Search and Linear Graph Algorithms”,
SIAM Journal on Computing, Vol. 1, No. 2, pp. 146-160, June 1972.
automatically generated by template/unicode_norm_gen.tmpl
In-memory session storage class.
Implements session storage as a global in-memory hash. Session
data will only persist for as long as the Ruby interpreter instance does.
A StoreContext
is used while validating a single certificate and holds the status involved.
The X509
certificate store holds trusted CA certificates used to verify peer certificates.
The easiest way to create a useful certificate store is:
cert_store = OpenSSL::X509::Store.new cert_store.set_default_paths
This will use your system’s built-in certificates.
If your system does not have a default set of certificates you can obtain a set extracted from Mozilla CA certificate store by cURL maintainers here: curl.haxx.se/docs/caextract.html (You may wish to use the firefox-db2pem.sh script to extract the certificates from a local install to avoid man-in-the-middle attacks.)
After downloading or generating a cacert.pem from the above link you can create a certificate store from the pem file like this:
cert_store = OpenSSL::X509::Store.new cert_store.add_file 'cacert.pem'
The certificate store can be used with an SSLSocket like this:
ssl_context = OpenSSL::SSL::SSLContext.new ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER ssl_context.cert_store = cert_store tcp_socket = TCPSocket.open 'example.com', 443 ssl_socket = OpenSSL::SSL::SSLSocket.new tcp_socket, ssl_context
File-based session storage class.
Implements session storage as a flat file of ‘key=value’ values. This storage type only works directly with String values; the user is responsible for converting other types to Strings when storing and from Strings when retrieving.
Dummy session storage class.
Implements session storage place holder. No actual storage will be done.
Authenticator for the “DIGEST-MD5” authentication type. See authenticate().
Error
raised upon a “NO” response from the server, indicating that the client command could not be completed successfully.
Raised when a bad requirement is encountered
The RequirementList
is used to hold the requirements being considered while resolving a set of gems.
The RequirementList
acts like a queue where the oldest items are removed first.
An absolutely silent progress reporter.
A basic dotted progress reporter.
A progress reporter that prints out messages about the current progress.
Raised by Encoding
and String methods when the source encoding is incompatible with the target encoding.
An FFI closure wrapper, for handling callbacks.
closure = Class.new(Fiddle::Closure) { def call 10 end }.new(Fiddle::TYPE_INT, []) #=> #<#<Class:0x0000000150d308>:0x0000000150d240> func = Fiddle::Function.new(closure, [], Fiddle::TYPE_INT) #=> #<Fiddle::Function:0x00000001516e58> func.call #=> 10
standard dynamic load exception