Returns a new Array containing all values in self
:
h = {foo: 0, bar: 1, baz: 2} h.values # => [0, 1, 2]
Method
value?
is an alias for #has_value?.
Returns true
if value
is a value in self
, otherwise false
.
Returns all environment variable values in an Array:
ENV.replace('foo' => '0', 'bar' => '1') ENV.values # => ['1', '0']
The order of the values is OS-dependent. See About Ordering.
Returns true
if value
is the value for some environment variable name, false
otherwise:
ENV.replace('foo' => '0', 'bar' => '1') ENV.value?('0') # => true ENV.has_value?('0') # => true ENV.value?('2') # => false ENV.has_value?('2') # => false
Reads at most maxlen bytes from the ARGF
stream.
If the optional outbuf argument is present, it must reference a String
, which will receive the data. The outbuf will contain only the received data after the method call even if it is not empty at the beginning.
It raises EOFError
on end of ARGF
stream. Since ARGF
stream is a concatenation of multiple files, internally EOF is occur for each file. ARGF.readpartial
returns empty strings for EOFs except the last one and raises EOFError
for the last one.
Returns the current offset (in bytes) of the current file in ARGF
.
ARGF.pos #=> 0 ARGF.gets #=> "This is line one\n" ARGF.pos #=> 17
Creates or retrieves cached CSV objects. For arguments and options, see CSV.new
.
This API is not Ractor-safe.
With no block given, returns a CSV object.
The first call to instance
creates and caches a CSV object:
s0 = 's0' csv0 = CSV.instance(s0) csv0.class # => CSV
Subsequent calls to instance
with that same string
or io
retrieve that same cached object:
csv1 = CSV.instance(s0) csv1.class # => CSV csv1.equal?(csv0) # => true # Same CSV object
A subsequent call to instance
with a different string
or io
creates and caches a different CSV object.
s1 = 's1' csv2 = CSV.instance(s1) csv2.equal?(csv0) # => false # Different CSV object
All the cached objects remains available:
csv3 = CSV.instance(s0) csv3.equal?(csv0) # true # Same CSV object csv4 = CSV.instance(s1) csv4.equal?(csv2) # true # Same CSV object
When a block is given, calls the block with the created or retrieved CSV object; returns the block’s return value:
CSV.instance(s0) {|csv| :foo } # => :foo
Calls CSV.read
with source
, options
, and certain default options:
headers
: true
converters
: :numeric
header_converters
: :symbol
Returns a CSV::Table
object.
Example:
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string) CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:4>
Evaluates the Ruby expression(s) in string, in the binding’s context. If the optional filename and lineno parameters are present, they will be used when reporting syntax errors.
def get_binding(param) binding end b = get_binding("hello") b.eval("param") #=> "hello"
Looks up the first IP address for name
.
Looks up all IP address for name
.
Looks up the first IP address for name
.
Looks up all IP address for name
.
Take a message from ractor’s outgoing port, which was put there by Ractor.yield
or at ractor’s finalization.
r = Ractor.new do Ractor.yield 'explicit yield' 'last value' end puts r.take #=> 'explicit yield' puts r.take #=> 'last value' puts r.take # Ractor::ClosedError (The outgoing-port is already closed)
The fact that the last value is also put to outgoing port means that take
can be used as some analog of Thread#join
(“just wait till ractor finishes”), but don’t forget it will raise if somebody had already consumed everything ractor have produced.
If the outgoing port was closed with close_outgoing
, the method will raise Ractor::ClosedError
.
r = Ractor.new do sleep(500) Ractor.yield 'Hello from ractor' end r.close_outgoing r.take # Ractor::ClosedError (The outgoing-port is already closed) # The error would be raised immediately, not when ractor will try to receive
If an uncaught exception is raised in the Ractor
, it is propagated on take as a Ractor::RemoteError
.
r = Ractor.new {raise "Something weird happened"} begin r.take rescue => e p e # => #<Ractor::RemoteError: thrown by remote Ractor.> p e.ractor == r # => true p e.cause # => #<RuntimeError: Something weird happened> end
Ractor::ClosedError
is a descendant of StopIteration
, so the closing of the ractor will break the loops without propagating the error:
r = Ractor.new do 3.times {|i| Ractor.yield "message #{i}"} "finishing" end loop {puts "Received: " + r.take} puts "Continue successfully"
This will print:
Received: message 0 Received: message 1 Received: message 2 Received: finishing Continue successfully
Basically the same as ::new
. However, if class Thread
is subclassed, then calling start
in that subclass will not invoke the subclass’s initialize
method.
Causes the given thread
to exit, see also Thread::exit
.
count = 0 a = Thread.new { loop { count += 1 } } sleep(0.1) #=> 0 Thread.kill(a) #=> #<Thread:0x401b3d30 dead> count #=> 93947 a.alive? #=> false
Waits for thr
to complete, using join
, and returns its value or raises the exception which terminated the thread.
a = Thread.new { 2 + 2 } a.value #=> 4 b = Thread.new { raise 'something went wrong' } b.value #=> RuntimeError: something went wrong
Terminates thr
and schedules another thread to be run, returning the terminated Thread
. If this is the main thread, or the last thread, exits the process.
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"
Returns true
if thr
is running or sleeping.
thr = Thread.new { } thr.join #=> #<Thread:0x401b3fb0 dead> Thread.current.alive? #=> true thr.alive? #=> false
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.
Return the tag object which was called for.
Return the return value which was called for.
Returns the \BigDecimal converted from +value+ with a precision of +ndigits+ decimal digits. When +ndigits+ is less than the number of significant digits in the value, the result is rounded to that number of digits, according to the current rounding mode; see BigDecimal.mode.
Returns value
converted to a BigDecimal, depending on the type of value
:
Integer
, Float
, Rational
, Complex
, or BigDecimal: converted directly:
# Integer, Complex, or BigDecimal value does not require ndigits; ignored if given. BigDecimal(2) # => 0.2e1 BigDecimal(Complex(2, 0)) # => 0.2e1 BigDecimal(BigDecimal(2)) # => 0.2e1 # Float or Rational value requires ndigits. BigDecimal(2.0, 0) # => 0.2e1 BigDecimal(Rational(2, 1), 0) # => 0.2e1
String: converted by parsing if it contains an integer or floating-point literal; leading and trailing whitespace is ignored:
# String does not require ndigits; ignored if given. BigDecimal('2') # => 0.2e1 BigDecimal('2.0') # => 0.2e1 BigDecimal('0.2e1') # => 0.2e1 BigDecimal(' 2.0 ') # => 0.2e1
Other type that responds to method :to_str
: first converted to a string, then converted to a BigDecimal, as above.
Other type:
Raises an exception if keyword argument exception
is true
.
Returns nil
if keyword argument exception
is true
.
Raises an exception if value
evaluates to a Float
and digits
is larger than Float::DIG + 1.