This class implements a pretty printing algorithm. It finds line breaks and nice indentations for grouped structure.
By default, the class assumes that primitive elements are strings and each byte in the strings have single column in width. But it can be used for other situations by giving suitable arguments for some methods:
newline object and space generation block for PrettyPrint.new
optional width argument for PrettyPrint#text
There are several candidate uses:
text formatting using proportional fonts
multibyte characters which has columns different to number of bytes
non-string formatting
Box based formatting?
Other (better) model/algorithm?
Report any bugs at bugs.ruby-lang.org
Christian Lindig, Strictly Pretty, March 2000, www.st.cs.uni-sb.de/~lindig/papers/#pretty
Philip Wadler, A prettier printer, March 1998, homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
Tanaka Akira <akr@fsij.org>
The set of all prime numbers.
Prime.each(100) do |prime| p prime #=> 2, 3, 5, 7, 11, ...., 97 end
Prime
is Enumerable:
Prime.first 5 # => [2, 3, 5, 7, 11]
For convenience, each instance method of Prime
.instance can be accessed as a class method of Prime
.
e.g.
Prime.instance.prime?(2) #=> true Prime.prime?(2) #=> true
A “generator” provides an implementation of enumerating pseudo-prime numbers and it remembers the position of enumeration and upper bound. Furthermore, it is an external iterator of prime enumeration which is compatible with an Enumerator
.
Prime
::PseudoPrimeGenerator
is the base class for generators. There are few implementations of generator.
Prime
::EratosthenesGenerator
Uses Eratosthenes’ sieve.
Prime
::TrialDivisionGenerator
Uses the trial division method.
Prime
::Generator23
Generates all positive integers which are not divisible by either 2 or 3. This sequence is very bad as a pseudo-prime sequence. But this is faster and uses much less memory than the other generators. So, it is suitable for factorizing an integer which is not large but has many prime factors. e.g. for Prime#prime?
.
ConditionVariable
objects augment class Mutex
. Using condition variables, it is possible to suspend while in the middle of a critical section until a resource becomes available.
Example:
mutex = Mutex.new resource = ConditionVariable.new a = Thread.new { mutex.synchronize { # Thread 'a' now needs the resource resource.wait(mutex) # 'a' can now have the resource } } b = Thread.new { mutex.synchronize { # Thread 'b' has finished using the resource resource.signal } }
A module to implement the Linda distributed computing paradigm in Ruby.
See the sample/drb/ directory in the Ruby distribution, from 1.8.2 onwards.
This library is an interface to secure random number generators which are suitable for generating session keys in HTTP cookies, etc.
You can use this library in your application by requiring it:
require 'securerandom'
It supports the following secure random number generators:
openssl
/dev/urandom
SecureRandom
is extended by the Random::Formatter
module which defines the following methods:
alphanumeric
base64
choose
hex
rand
random_bytes
random_number
urlsafe_base64
uuid
These methods are usable as class methods of SecureRandom
such as ‘SecureRandom.hex`.
Generate random hexadecimal strings:
require 'securerandom' SecureRandom.hex(10) #=> "52750b30ffbc7de3b362" SecureRandom.hex(10) #=> "92b15d6c8dc4beb5f559" SecureRandom.hex(13) #=> "39b290146bea6ce975c37cfc23"
Generate random base64 strings:
SecureRandom.base64(10) #=> "EcmTPZwWRAozdA==" SecureRandom.base64(10) #=> "KO1nIU+p9DKxGg==" SecureRandom.base64(12) #=> "7kJSM/MzBJI+75j8"
Generate random binary strings:
SecureRandom.random_bytes(10) #=> "\016\t{\370g\310pbr\301" SecureRandom.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
Generate alphanumeric strings:
SecureRandom.alphanumeric(10) #=> "S8baxMJnPl" SecureRandom.alphanumeric(10) #=> "aOxAg8BAJe"
Generate UUIDs:
SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594" SecureRandom.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
Generates URL-encoded form data from given enum
.
This generates application/x-www-form-urlencoded data defined in HTML5 from given an Enumerable
object.
This internally uses URI.encode_www_form_component(str)
.
This method doesn’t convert the encoding of given items, so convert them before calling this method if you want to send data as other than original encoding or mixed encoding data. (Strings which are encoded in an HTML5 ASCII incompatible encoding are converted to UTF-8.)
This method doesn’t handle files. When you send a file, use multipart/form-data.
This refers url.spec.whatwg.org/#concept-urlencoded-serializer
URI.encode_www_form([["q", "ruby"], ["lang", "en"]]) #=> "q=ruby&lang=en" URI.encode_www_form("q" => "ruby", "lang" => "en") #=> "q=ruby&lang=en" URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en") #=> "q=ruby&q=perl&lang=en" URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]]) #=> "q=ruby&q=perl&lang=en"
Decodes URL-encoded form data from given str
.
This decodes application/x-www-form-urlencoded data and returns an array of key-value arrays.
This refers url.spec.whatwg.org/#concept-urlencoded-parser, so this supports only &-separator, and doesn’t support ;-separator.
ary = URI.decode_www_form("a=1&a=2&b=3") ary #=> [['a', '1'], ['a', '2'], ['b', '3']] ary.assoc('a').last #=> '1' ary.assoc('b').last #=> '3' ary.rassoc('a').last #=> '2' Hash[ary] #=> {"a"=>"2", "b"=>"3"}
WIN32OLE_VARIABLE
objects represent OLE variable information.
WIN32OLE_VARIANT
objects represents OLE variant.
Win32OLE converts Ruby object into OLE variant automatically when invoking OLE methods. If OLE method requires the argument which is different from the variant by automatic conversion of Win32OLE, you can convert the specfied variant type by using WIN32OLE_VARIANT
class.
param = WIN32OLE_VARIANT.new(10, WIN32OLE::VARIANT::VT_R4) oleobj.method(param)
WIN32OLE_VARIANT
does not support VT_RECORD variant. Use WIN32OLE_RECORD
class instead of WIN32OLE_VARIANT
if the VT_RECORD variant is needed.
SecHandle struct
Creates binary representations of a SecBufferDesc structure, including the SecBuffer contained inside.
This is not an existing class, but documentation of the interface that Scheduler object should comply in order to be used as 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 waited for, 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 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 (I/O ready, sleep time passed).
A typical implementation would probably rely for this closing loop on a gem like EventMachine or Async.
This way concurrent execution will be achieved in a way that is transparent for every individual Fiber’s code.
Hook methods are:
(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 suggested that the scheduler implement 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
Enumerator::ArithmeticSequence
is a subclass of Enumerator
, that is a representation of sequences of numbers with common difference. Instances of this class can be generated by the Range#step
and Numeric#step
methods.
The class can be used for slicing Array
(see Array#slice
) or custom collections.
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
FIXME: This isn’t documented in Nutshell.
Since MonitorMixin.new_cond
returns a ConditionVariable
, and the example above calls while_wait and signal, this class should be documented.
UDP/IP address information used by Socket.udp_server_loop
.
Zlib::GzipWriter
is a class for writing gzipped files. GzipWriter
should be used with an instance of IO
, or IO-like, object.
Following two example generate the same result.
Zlib::GzipWriter.open('hoge.gz') do |gz| gz.write 'jugemu jugemu gokou no surikire...' end File.open('hoge.gz', 'w') do |f| gz = Zlib::GzipWriter.new(f) gz.write 'jugemu jugemu gokou no surikire...' gz.close end
To make like gzip(1) does, run following:
orig = 'hoge.txt' Zlib::GzipWriter.open('hoge.gz') do |gz| gz.mtime = File.mtime(orig) gz.orig_name = orig gz.write IO.binread(orig) end
NOTE: Due to the limitation of Ruby’s finalizer, you must explicitly close GzipWriter
objects by Zlib::GzipWriter#close
etc. Otherwise, GzipWriter
will be not able to write the gzip footer and will generate a broken gzip file.
exception to wait for writing by EAGAIN. see IO.select
.
exception to wait for writing by EWOULDBLOCK. see IO.select
.
exception to wait for writing by EINPROGRESS. see IO.select
.
Note: Don’t use this class directly. This is an internal class.