Returns the imaginary part of the matrix.
Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]] # => 1+2i i 0 # 1 2 3 Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].imaginary # => 2i i 0 # 0 0 0
Creates a vector from an Array
. The optional second argument specifies whether the array itself or a copy is used internally.
Initializes a new instance and evaluates the optional block in context of the instance. Arguments args
are passed to new
, see there for description of parameters.
This method is deprecated, its behavior corresponds to the older new
method.
Parses command line arguments argv
in order when environment variable POSIXLY_CORRECT is set, and in permutation mode otherwise. When optional into
keyword argument is provided, the parsed option values are stored there via []=
method (so it can be Hash
, or OpenStruct
, or other similar object).
Same as parse
, but removes switches destructively. Non-option arguments remain in argv
.
Parses environment variable env
or its uppercase with splitting like a shell.
env
defaults to the basename of the program.
Returns the number of elements in the match array.
m = /(.)(.)(\d+)(\d)/.match("THX1138.") m.length #=> 5 m.size #=> 5
This is a convenience method which is same as follows:
begin q = PrettyPrint.new(output, maxwidth, newline, &genspace) ... q.flush output end
Returns true if value
is a prime number, else returns false. Integer#prime?
is much more performant.
value
an arbitrary integer to be checked.
generator
optional. A pseudo-prime generator.
Creates a temporary file as a usual File
object (not a Tempfile
). It does not use finalizer and delegation, which makes it more efficient and reliable.
If no block is given, this is similar to Tempfile.new
except creating File
instead of Tempfile
. In that case, the created file is not removed automatically. You should use File.unlink
to remove it.
If a block is given, then a File
object will be constructed, and the block is invoked with the object as the argument. The File
object will be automatically closed and the temporary file is removed after the block terminates, releasing all resources that the block created. The call returns the value of the block.
In any case, all arguments (basename
, tmpdir
, mode
, and **options
) will be treated the same as for Tempfile.new
.
Tempfile.create('foo', '/home/temp') do |f| # ... do something with f ... end
Returns true
if a Proc
object is lambda. false
if non-lambda.
The lambda-ness affects argument handling and the behavior of return
and break
.
A Proc
object generated by proc
ignores extra arguments.
proc {|a,b| [a,b] }.call(1,2,3) #=> [1,2]
It provides nil
for missing arguments.
proc {|a,b| [a,b] }.call(1) #=> [1,nil]
It expands a single array argument.
proc {|a,b| [a,b] }.call([1,2]) #=> [1,2]
A Proc
object generated by lambda
doesn’t have such tricks.
lambda {|a,b| [a,b] }.call(1,2,3) #=> ArgumentError lambda {|a,b| [a,b] }.call(1) #=> ArgumentError lambda {|a,b| [a,b] }.call([1,2]) #=> ArgumentError
Proc#lambda?
is a predicate for the tricks. It returns true
if no tricks apply.
lambda {}.lambda? #=> true proc {}.lambda? #=> false
Proc.new
is the same as proc
.
Proc.new {}.lambda? #=> false
lambda
, proc
and Proc.new
preserve the tricks of a Proc
object given by &
argument.
lambda(&lambda {}).lambda? #=> true proc(&lambda {}).lambda? #=> true Proc.new(&lambda {}).lambda? #=> true lambda(&proc {}).lambda? #=> false proc(&proc {}).lambda? #=> false Proc.new(&proc {}).lambda? #=> false
A Proc
object generated by &
argument has the tricks
def n(&b) b.lambda? end n {} #=> false
The &
argument preserves the tricks if a Proc
object is given by &
argument.
n(&lambda {}) #=> true n(&proc {}) #=> false n(&Proc.new {}) #=> false
A Proc
object converted from a method has no tricks.
def m() end method(:m).to_proc.lambda? #=> true n(&method(:m)) #=> true n(&method(:m).to_proc) #=> true
define_method
is treated the same as method definition. The defined method has no tricks.
class C define_method(:d) {} end C.new.d(1,2) #=> ArgumentError C.new.method(:d).to_proc.lambda? #=> true
define_method
always defines a method without the tricks, even if a non-lambda Proc
object is given. This is the only exception for which the tricks are not preserved.
class C define_method(:e, &proc {}) end C.new.e(1,2) #=> ArgumentError C.new.method(:e).to_proc.lambda? #=> true
This exception ensures that methods never have tricks and makes it easy to have wrappers to define methods that behave as usual.
class C def self.def2(name, &body) define_method(name, &body) end def2(:f) {} end C.new.f(1,2) #=> ArgumentError
The wrapper def2 defines a method which has no tricks.
Give the thread scheduler a hint to pass execution to another thread. A running thread may or may not switch, it depends on OS and processor.
Returns the status of thr
.
"sleep"
Returned if this thread is sleeping or waiting on I/O
"run"
When this thread is executing
"aborting"
If this thread is aborting
false
When this thread is terminated normally
nil
If terminated with an exception.
a = Thread.new { raise("die now") } b = Thread.new { Thread.stop } c = Thread.new { Thread.exit } d = Thread.new { sleep } d.kill #=> #<Thread:0x401b3678 aborting> a.status #=> nil b.status #=> "sleep" c.status #=> false d.status #=> "aborting" Thread.current.status #=> "run"
Wakes up the first thread in line waiting for this lock.
Returns the length of the queue.
Returns the length of the queue.
Returns internal information of TracePoint
.
The contents of the returned value are implementation specific. It may be changed in future.
This method is only for debugging TracePoint
itself.
Activates the trace.
Returns true
if trace was enabled. Returns false
if trace was disabled.
trace.enabled? #=> false trace.enable #=> false (previous state) # trace is enabled trace.enabled? #=> true trace.enable #=> true (previous state) # trace is still enabled
If a block is given, the trace will only be enabled within the scope of the block.
trace.enabled? #=> false trace.enable do trace.enabled? # only enabled for this block end trace.enabled? #=> false
target
, target_line
and target_thread
parameters are used to limit tracing only to specified code objects. target
should be a code object for which RubyVM::InstructionSequence.of
will return an instruction sequence.
t = TracePoint.new(:line) { |tp| p tp } def m1 p 1 end def m2 p 2 end t.enable(target: method(:m1)) m1 # prints #<TracePoint:line test.rb:4 in `m1'> m2 # prints nothing
Note: You cannot access event hooks within the enable
block.
trace.enable { p tp.lineno } #=> RuntimeError: access from outside
The current status of the trace
In (min, max)
form, returns min if obj <=>
min is less than zero, max if obj <=>
max is greater than zero, and obj otherwise.
12.clamp(0, 100) #=> 12 523.clamp(0, 100) #=> 100 -3.123.clamp(0, 100) #=> 0 'd'.clamp('a', 'f') #=> 'd' 'z'.clamp('a', 'f') #=> 'f'
In (range)
form, returns range.begin if obj <=>
range.begin is less than zero, range.end if obj <=>
range.end is greater than zero, and obj otherwise.
12.clamp(0..100) #=> 12 523.clamp(0..100) #=> 100 -3.123.clamp(0..100) #=> 0 'd'.clamp('a'..'f') #=> 'd' 'z'.clamp('a'..'f') #=> 'f'
If range.begin is nil
, it is considered smaller than obj, and if range.end is nil
, it is considered greater than obj.
-20.clamp(0..) #=> 0 523.clamp(..100) #=> 100
When range.end is excluded and not nil
, an exception is raised.
100.clamp(0...100) # ArgumentError
Yields self to the block and returns the result of the block.
3.next.then {|x| x**x }.to_s #=> "256"
Good usage for then
is value piping in method chains:
require 'open-uri' require 'json' construct_url(arguments). then {|url| open(url).read }. then {|response| JSON.parse(response) }
When called without block, the method returns Enumerator
, which can be used, for example, for conditional circuit-breaking:
# meets condition, no-op 1.then.detect(&:odd?) # => 1 # does not meet condition, drop value 2.then.detect(&:odd?) # => nil
Returns arg converted to a float. Numeric
types are converted directly, and with exception to String
and nil
the rest are converted using arg.to_f
. Converting a String
with invalid characters will result in a ArgumentError
. Converting nil
generates a TypeError
. Exceptions can be suppressed by passing exception: false
.
Float(1) #=> 1.0 Float("123.456") #=> 123.456 Float("123.0_badstring") #=> ArgumentError: invalid value for Float(): "123.0_badstring" Float(nil) #=> TypeError: can't convert nil into Float Float("123.0_badstring", exception: false) #=> nil