Constructors for classes defined with ::define
accept both positional and keyword arguments.
Measure = Data.define(:amount, :unit) Measure.new(1, 'km') #=> #<data Measure amount=1, unit="km"> Measure.new(amount: 1, unit: 'km') #=> #<data Measure amount=1, unit="km"> # Alternative shorter intialization with [] Measure[1, 'km'] #=> #<data Measure amount=1, unit="km"> Measure[amount: 1, unit: 'km'] #=> #<data Measure amount=1, unit="km">
All arguments are mandatory (unlike Struct
), and converted to keyword arguments:
Measure.new(amount: 1) # in `initialize': missing keyword: :unit (ArgumentError) Measure.new(1) # in `initialize': missing keyword: :unit (ArgumentError)
Note that Measure#initialize
always receives keyword arguments, and that mandatory arguments are checked in initialize
, not in new
. This can be important for redefining initialize in order to convert arguments or provide defaults:
Measure = Data.define(:amount, :unit) do NONE = Data.define def initialize(amount:, unit: NONE.new) super(amount: Float(amount), unit:) end end Measure.new('10', 'km') # => #<data Measure amount=10.0, unit="km"> Measure.new(10_000) # => #<data Measure amount=10000.0, unit=#<data NONE>>
Creates a buffer for pretty printing.
output
is an output target. If it is not specified, ” is assumed. It should have a << method which accepts the first argument obj
of PrettyPrint#text
, the first argument sep
of PrettyPrint#breakable
, the first argument newline
of PrettyPrint.new
, and the result of a given block for PrettyPrint.new
.
maxwidth
specifies maximum line length. If it is not specified, 79 is assumed. However actual outputs may overflow maxwidth
if long non-breakable texts are provided.
newline
is used for line breaks. “n” is used if it is not specified.
The block is used to generate spaces. {|width| ‘ ’ * width} is used if it is not given.
Returns a new PStore object.
Argument file
is the path to the file in which objects are to be stored; if the file exists, it should be one that was written by PStore.
path = 't.store' store = PStore.new(path)
A PStore object is reentrant. If argument thread_safe
is given as true
, the object is also thread-safe (at the cost of a small performance penalty):
store = PStore.new(path, true)
Creates a new Resolv
using resolvers
.
Creates a file in the underlying file system; returns a new Tempfile object based on that file.
If possible, consider instead using Tempfile.create
, which:
Avoids the performance cost of delegation, incurred when Tempfile.new
calls its superclass DelegateClass(File)
.
Does not rely on a finalizer to close and unlink the file, which can be unreliable.
Creates and returns file whose:
Class
is Tempfile (not File, as in Tempfile.create
).
Directory is the system temporary directory (system-dependent).
Generated filename is unique in that directory.
Permissions are 0600
; see File Permissions.
Mode is 'w+'
(read/write mode, positioned at the end).
The underlying file is removed when the Tempfile object dies and is reclaimed by the garbage collector.
Example:
f = Tempfile.new # => #<Tempfile:/tmp/20220505-17839-1s0kt30> f.class # => Tempfile f.path # => "/tmp/20220505-17839-1s0kt30" f.stat.mode.to_s(8) # => "100600" File.exist?(f.path) # => true File.unlink(f.path) # File.exist?(f.path) # => false
Argument basename
, if given, may be one of:
A string: the generated filename begins with basename
:
Tempfile.new('foo') # => #<Tempfile:/tmp/foo20220505-17839-1whk2f>
An array of two strings [prefix, suffix]
: the generated filename begins with prefix
and ends with suffix
:
Tempfile.new(%w/foo .jpg/) # => #<Tempfile:/tmp/foo20220505-17839-58xtfi.jpg>
With arguments basename
and tmpdir
, the file is created in directory tmpdir
:
Tempfile.new('foo', '.') # => #<Tempfile:./foo20220505-17839-xfstr8>
Keyword arguments mode
and options
are passed directly to method File.open
:
The value given with mode
must be an integer, and may be expressed as the logical OR of constants defined in File::Constants
.
For options
, see Open Options.
Related: Tempfile.create
.
Creates a weak reference to orig
Creates a new Proc
object, bound to the current context.
proc = Proc.new { "hello" } proc.call #=> "hello"
Raises ArgumentError
if called without a block.
Proc.new #=> ArgumentError
Create a new Ractor
with args and a block.
A block (Proc
) will be isolated (can’t access to outer variables). self
inside the block will refer to the current Ractor
.
r = Ractor.new { puts "Hi, I am #{self.inspect}" } r.take # Prints "Hi, I am #<Ractor:#2 test.rb:1 running>"
args
passed to the method would be propagated to block args by the same rules as objects passed through send
/Ractor.receive: if args
are not shareable, they will be copied (via deep cloning, which might be inefficient).
arg = [1, 2, 3] puts "Passing: #{arg} (##{arg.object_id})" r = Ractor.new(arg) {|received_arg| puts "Received: #{received_arg} (##{received_arg.object_id})" } r.take # Prints: # Passing: [1, 2, 3] (#280) # Received: [1, 2, 3] (#300)
Ractor’s name
can be set for debugging purposes:
r = Ractor.new(name: 'my ractor') {} p r #=> #<Ractor:#3 my ractor test.rb:1 terminated>
Creates a new PRNG using seed
to set the initial state. If seed
is omitted, the generator is initialized with Random.new_seed
.
See Random.srand
for more information on the use of seed values.
Creates a new thread executing the given block.
Any args
given to ::new
will be passed to the block:
arr = [] a, b, c = 1, 2, 3 Thread.new(a,b,c) { |d,e,f| arr << d << e << f }.join arr #=> [1, 2, 3]
A ThreadError
exception is raised if ::new
is called without a block.
If you’re going to subclass Thread
, be sure to call super in your initialize
method, otherwise a ThreadError
will be raised.
Returns a new TracePoint
object, not enabled by default.
Next, in order to activate the trace, you must use TracePoint#enable
trace = TracePoint.new(:call) do |tp| p [tp.lineno, tp.defined_class, tp.method_id, tp.event] end #=> #<TracePoint:disabled> trace.enable #=> false puts "Hello, TracePoint!" # ... # [48, IRB::Notifier::AbstractNotifier, :printf, :call] # ...
When you want to deactivate the trace, you must use TracePoint#disable
trace.disable
See Events at TracePoint
for possible events and more information.
A block must be given, otherwise an ArgumentError
is raised.
If the trace method isn’t included in the given events filter, a RuntimeError
is raised.
TracePoint.trace(:line) do |tp| p tp.raised_exception end #=> RuntimeError: 'raised_exception' not supported by this event
If the trace method is called outside block, a RuntimeError
is raised.
TracePoint.trace(:line) do |tp| $tp = tp end $tp.lineno #=> access from outside (RuntimeError)
Access from other threads is also forbidden.
Document-class: UncaughtThrowError
Raised when throw
is called with a tag which does not have corresponding catch
block.
throw "foo", "bar"
raises the exception:
UncaughtThrowError: uncaught throw "foo"
Use extend MonitorMixin
or include MonitorMixin
instead of this constructor. Have look at the examples above to understand how to use this module.
Returns a copy of self
with the given start
value:
d0 = Date.new(2000, 2, 3) d0.julian? # => false d1 = d0.new_start(Date::JULIAN) d1.julian? # => true
See argument start.
This string is put at the end of a line that holds a JSON
array.
This string is put at the end of a line that holds a JSON
array.
Duplicates self and resets its offset.
d = DateTime.new(2001,2,3,4,5,6,'-02:00') #=> #<DateTime: 2001-02-03T04:05:06-02:00 ...> d.new_offset('+09:00') #=> #<DateTime: 2001-02-03T15:05:06+09:00 ...>
Returns a new binding each time near TOPLEVEL_BINDING for runs that do not specify a binding.
Creates a new ipaddr containing the given network byte ordered string form of an IP address.
Returns an arbitrary seed value. This is used by Random.new
when no seed value is specified as an argument.
Random.new_seed #=> 115032730400174366788466674494640623225
Creates a new MonitorMixin::ConditionVariable
associated with the Monitor
object.
newton.rb
Solves the nonlinear algebraic equation system f = 0 by Newton’s method. This program is not dependent on BigDecimal
.
To call:
n = nlsolve(f,x) where n is the number of iterations required, x is the initial value vector f is an Object which is used to compute the values of the equations to be solved.
It must provide the following methods:
returns the values of all functions at x
returns 0.0
returns 1.0
returns 2.0
returns 10.0
returns the convergence criterion (epsilon value) used to determine whether two values are considered equal. If |a-b| < epsilon, the two values are considered equal.
On exit, x is the solution vector.