The command manager registers and installs all the individual sub-commands supported by the gem command.
Extra commands can be provided by writing a rubygems_plugin.rb file in an installed gem. You should register your command against the Gem::CommandManager
instance, like this:
# file rubygems_plugin.rb require 'rubygems/command_manager' Gem::CommandManager.instance.register_command :edit
You should put the implementation of your command in rubygems/commands.
# file rubygems/commands/edit_command.rb class Gem::Commands::EditCommand < Gem::Command # ... end
See Gem::Command
for instructions on writing gem commands.
Raised when RubyGems is unable to load or activate a gem. Contains the name and version requirements of the gem that either conflicts with already activated gems or that RubyGems is otherwise unable to activate.
Raised when trying to activate a gem, and the gem exists on the system, but not the requested version. Instead of rescuing from this class, make sure to rescue from the superclass Gem::LoadError
to catch all types of load errors.
Signals that a file permission error is preventing the user from operating on the given directory.
Raised by Gem::Validator
when something is not right in a gem.
Run an instance of the gem program.
Gem::GemRunner
is only intended for internal use by RubyGems itself. It does not form any public API and may change at any time for any reason.
If you would like to duplicate functionality of ‘gem` commands, use the classes they call directly.
Gem::Server
and allows users to serve gems for consumption by ‘gem –remote-install`.
gem_server starts an HTTP server on the given port and serves the following:
“/” - Browsing of gem spec files for installed gems
“/specs.#{Gem.marshal_version}.gz” - specs name/version/platform index
“/latest_specs.#{Gem.marshal_version}.gz” - latest specs name/version/platform index
“/quick/” - Individual gemspecs
“/gems” - Direct access to download the installable gems
“/rdoc?q=” - Search for installed rdoc documentation
gem_server = Gem::Server.new Gem.dir, 8089, false gem_server.run
An HTTP Proxy server which proxies GET, HEAD and POST requests.
To create a simple proxy server:
require 'webrick' require 'webrick/httpproxy' proxy = WEBrick::HTTPProxyServer.new Port: 8000 trap 'INT' do proxy.shutdown end trap 'TERM' do proxy.shutdown end proxy.start
See ::new
for proxy-specific configuration items.
To modify content the proxy server returns use the :ProxyContentHandler
option:
handler = proc do |req, res| if res['content-type'] == 'text/plain' then res.body << "\nThis content was proxied!\n" end end proxy = WEBrick::HTTPProxyServer.new Port: 8000, ProxyContentHandler: handler
An HTTP Server
A generic logging class
A logging class that prepends a timestamp to each message.
Base server class
Raised by Encoding
and String methods when a transcoding operation fails.
Utility methods for using the RubyGems API.
When rubygems/test_case is required the default user interaction is a MockGemUi
.
Module
that defines the default UserInteraction
. Any class including this module will have access to the ui
method that returns the default UI.
UserInteraction
allows RubyGems to interact with the user through standard methods that can be replaced with more-specific UI methods for different displays.
Since UserInteraction
dispatches to a concrete UI class you may need to reference other classes for specific behavior such as Gem::ConsoleUI
or Gem::SilentUI
.
Example:
class X include Gem::UserInteraction def get_answer n = ask("What is the meaning of life?") end end
AccessLog
provides logging to various files in various formats.
Multiple logs may be written to at the same time:
access_log = [ [$stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT], [$stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT], ] server = WEBrick::HTTPServer.new :AccessLog => access_log
Custom log formats may be defined. WEBrick::AccessLog
provides a subset of the formatting from Apache’s mod_log_config httpd.apache.org/docs/mod/mod_log_config.html#formats. See AccessLog::setup_params for a list of supported options
Numeric
is the class from which all higher-level numeric classes should inherit.
Numeric
allows instantiation of heap-allocated objects. Other core numeric classes such as Integer
are implemented as immediates, which means that each Integer
is a single immutable object which is always passed by value.
a = 1 puts 1.object_id == a.object_id #=> true
There can only ever be one instance of the integer 1
, for example. Ruby ensures this by preventing instantiation. If duplication is attempted, the same instance is returned.
Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class 1.dup #=> 1 1.object_id == 1.dup.object_id #=> true
For this reason, Numeric
should be used when defining other numeric classes.
Classes which inherit from Numeric
must implement coerce
, which returns a two-member Array containing an object that has been coerced into an instance of the new class and self
(see coerce
).
Inheriting classes should also implement arithmetic operator methods (+
, -
, *
and /
) and the <=>
operator (see Comparable
). These methods may rely on coerce
to ensure interoperability with instances of other numeric classes.
class Tally < Numeric def initialize(string) @string = string end def to_s @string end def to_i @string.size end def coerce(other) [self.class.new('|' * other.to_i), self] end def <=>(other) to_i <=> other.to_i end def +(other) self.class.new('|' * (to_i + other.to_i)) end def -(other) self.class.new('|' * (to_i - other.to_i)) end def *(other) self.class.new('|' * (to_i * other.to_i)) end def /(other) self.class.new('|' * (to_i / other.to_i)) end end tally = Tally.new('||') puts tally * 2 #=> "||||" puts tally > 1 #=> true
Float
objects represent inexact real numbers using the native architecture’s double-precision floating point representation.
Floating point has a different arithmetic and is an inexact number. So you should know its esoteric system. see following:
Fibers are primitives for implementing light weight cooperative concurrency in Ruby. Basically they are a means of creating code blocks that can be paused and resumed, much like threads. The main difference is that they are never preempted and that the scheduling must be done by the programmer and not the VM.
As opposed to other stackless light weight concurrency models, each fiber comes with a stack. This enables the fiber to be paused from deeply nested function calls within the fiber block. See the ruby(1) manpage to configure the size of the fiber stack(s).
When a fiber is created it will not run automatically. Rather it must be explicitly asked to run using the Fiber#resume
method. The code running inside the fiber can give up control by calling Fiber.yield
in which case it yields control back to caller (the caller of the Fiber#resume
).
Upon yielding or termination the Fiber
returns the value of the last executed expression
For instance:
fiber = Fiber.new do Fiber.yield 1 2 end puts fiber.resume puts fiber.resume puts fiber.resume
produces
1 2 FiberError: dead fiber called
The Fiber#resume
method accepts an arbitrary number of parameters, if it is the first call to resume
then they will be passed as block arguments. Otherwise they will be the return value of the call to Fiber.yield
Example:
fiber = Fiber.new do |first| second = Fiber.yield first + 2 end puts fiber.resume 10 puts fiber.resume 14 puts fiber.resume 18
produces
12 14 FiberError: dead fiber called
A class which allows both internal and external iteration.
An Enumerator
can be created by the following methods.
Kernel#to_enum
Kernel#enum_for
Most methods have two forms: a block form where the contents are evaluated for each item in the enumeration, and a non-block form which returns a new Enumerator
wrapping the iteration.
enumerator = %w(one two three).each puts enumerator.class # => Enumerator enumerator.each_with_object("foo") do |item, obj| puts "#{obj}: #{item}" end # foo: one # foo: two # foo: three enum_with_obj = enumerator.each_with_object("foo") puts enum_with_obj.class # => Enumerator enum_with_obj.each do |item, obj| puts "#{obj}: #{item}" end # foo: one # foo: two # foo: three
This allows you to chain Enumerators together. For example, you can map a list’s elements to strings containing the index and the element as a string via:
puts %w[foo bar baz].map.with_index { |w, i| "#{i}:#{w}" } # => ["0:foo", "1:bar", "2:baz"]
An Enumerator
can also be used as an external iterator. For example, Enumerator#next
returns the next value of the iterator or raises StopIteration
if the Enumerator
is at the end.
e = [1,2,3].each # returns an enumerator object. puts e.next # => 1 puts e.next # => 2 puts e.next # => 3 puts e.next # raises StopIteration
You can use this to implement an internal iterator as follows:
def ext_each(e) while true begin vs = e.next_values rescue StopIteration return $!.result end y = yield(*vs) e.feed y end end o = Object.new def o.each puts yield puts yield(1) puts yield(1, 2) 3 end # use o.each as an internal iterator directly. puts o.each {|*x| puts x; [:b, *x] } # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3 # convert o.each to an external iterator for # implementing an internal iterator. puts ext_each(o.to_enum) {|*x| puts x; [:b, *x] } # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3
Raised to stop the iteration, in particular by Enumerator#next
. It is rescued by Kernel#loop
.
loop do puts "Hello" raise StopIteration puts "World" end puts "Done!"
produces:
Hello Done!