With a block given, calls the block with each remaining character in the stream; see Character IO.
With no block given, returns an enumerator.
With an argument, a block, or both given, derives a new hash new_hash
from self
, the argument, and/or the block; all, some, or none of its keys may be different from those in self
.
With a block given and no argument, new_hash
has keys determined only by the block.
For each key/value pair old_key/value
in self
, calls the block with old_key
; the block’s return value becomes new_key
; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys {|old_key| old_key.to_s } # => {"foo" => 0, "bar" => 1, "baz" => 2} h.transform_keys {|old_key| 'xxx' } # => {"xxx" => 2}
With argument other_hash
given and no block, new_hash
may have new keys provided by other_hash
and unchanged keys provided by self
.
For each key/value pair old_key/old_value
in self
, looks for key old_key
in other_hash
:
If old_key
is found, its value other_hash[old_key]
is taken as new_key
; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO) # => {FOO: 0, BAR: 1, BAZ: 2} h.transform_keys(baz: :FOO, bar: :FOO, foo: :FOO) # => {FOO: 2}
If old_key
is not found, sets new_hash[old_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys({}) # => {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :foo) # => {foo: 2, bar: 1}
Unused keys in other_hash
are ignored:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(bat: 3) # => {foo: 0, bar: 1, baz: 2}
With both argument other_hash
and a block given, new_hash
has new keys specified by other_hash
or by the block, and unchanged keys provided by self
.
For each pair old_key
and value
in self
:
If other_hash
has key old_key
(with value new_key
), does not call the block for that key; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' } # => {FOO: 0, BAR: 1, BAZ: 2}
If other_hash
does not have key old_key
, calls the block with old_key
and takes its return value as new_key
; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :BAZ) {|key| key.to_s.reverse } # => {"oof" => 0, "rab" => 1, BAZ: 2} h.transform_keys(baz: :BAZ) {|key| 'ook' } # => {"ook" => 1, BAZ: 2}
With no argument and no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
With an argument, a block, or both given, derives keys from the argument, the block, and self
; all, some, or none of the keys in self
may be changed.
With a block given and no argument, derives keys only from the block; all, some, or none of the keys in self
may be changed.
For each key/value pair old_key/value
in self
, calls the block with old_key
; the block’s return value becomes new_key
; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys! {|old_key| old_key.to_s } # => {"foo" => 0, "bar" => 1, "baz" => 2} h = {foo: 0, bar: 1, baz: 2} h.transform_keys! {|old_key| 'xxx' } # => {"xxx" => 2}
With argument other_hash
given and no block, derives keys for self
from other_hash
and self
; all, some, or none of the keys in self
may be changed.
For each key/value pair old_key/old_value
in self
, looks for key old_key
in other_hash
:
If old_key
is found, takes value other_hash[old_key]
as new_key
; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) # => {FOO: 0, BAR: 1, BAZ: 2} h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :FOO, bar: :FOO, foo: :FOO) # => {FOO: 2}
If old_key
is not found, does nothing:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!({}) # => {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :foo) # => {foo: 2, bar: 1}
Unused keys in other_hash
are ignored:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(bat: 3) # => {foo: 0, bar: 1, baz: 2}
With both argument other_hash
and a block given, derives keys from other_hash
, the block, and self
; all, some, or none of the keys in self
may be changed.
For each pair old_key
and value
in self
:
If other_hash
has key old_key
(with value new_key
), does not call the block for that key; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' } # => {FOO: 0, BAR: 1, BAZ: 2}
If other_hash
does not have key old_key
, calls the block with old_key
and takes its return value as new_key
; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ) {|key| key.to_s.reverse } # => {"oof" => 0, "rab" => 1, BAZ: 2} h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ) {|key| 'ook' } # => {"ook" => 1, BAZ: 2}
With no argument and no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
With a block given, returns a new hash new_hash
; for each pair key
/value
in self
, calls the block with value
and captures its return as new_value
; adds to new_hash
the entry key
/new_value
:
h = {foo: 0, bar: 1, baz: 2} h1 = h.transform_values {|value| value * 100} h1 # => {foo: 0, bar: 100, baz: 200}
With no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
With a block given, changes the values of self
as determined by the block; returns self
.
For each entry key
/old_value
in self
, calls the block with old_value
, captures its return value as new_value
, and sets self[key] = new_value
:
h = {foo: 0, bar: 1, baz: 2} h.transform_values! {|value| value * 100} # => {foo: 0, bar: 100, baz: 200}
With no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
Iterates over each character of each file in ARGF
.
This method allows you to treat the files supplied on the command line as a single file consisting of the concatenation of each named file. After the last character of the first file has been returned, the first character of the second file is returned. The ARGF.filename
method can be used to determine the name of the file in which the current character appears.
If no block is given, an enumerator is returned instead.
Serialization support for the object returned by _getobj_.
Reinitializes delegation from a serialized object.
Can be used to set eoutvar as described in ERB::new
. It’s probably easier to just use the constructor though, since calling this method requires the setup of an ERB
compiler object.
Returns a string for DNS reverse lookup compatible with RFC3172.
Creates a Range
object for the network address.
Returns the wildcard mask in string format e.g. 0.0.255.255
Program name to be emitted in error message and default banner, defaults to $0.
Returns the sharing detection flag as a boolean value. It is false (nil) by default.
Sets the sharing detection flag to b.
Returns the group most recently added to the stack.
Contrived example:
out = "" => "" q = PrettyPrint.new(out) => #<PrettyPrint:0x82f85c0 @output="", @maxwidth=79, @newline="\n", @genspace=#<Proc:0x82f8368@/home/vbatts/.rvm/rubies/ruby-head/lib/ruby/2.0.0/prettyprint.rb:82 (lambda)>, @output_width=0, @buffer_width=0, @buffer=[], @group_stack=[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>], @group_queue=#<PrettyPrint::GroupQueue:0x82fb7c0 @queue=[[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>]]>, @indent=0> q.group { q.text q.current_group.inspect q.text q.newline q.group(q.current_group.depth + 1) { q.text q.current_group.inspect q.text q.newline q.group(q.current_group.depth + 1) { q.text q.current_group.inspect q.text q.newline q.group(q.current_group.depth + 1) { q.text q.current_group.inspect q.text q.newline } } } } => 284 puts out #<PrettyPrint::Group:0x8354758 @depth=1, @breakables=[], @break=false> #<PrettyPrint::Group:0x8354550 @depth=2, @breakables=[], @break=false> #<PrettyPrint::Group:0x83541cc @depth=3, @breakables=[], @break=false> #<PrettyPrint::Group:0x8347e54 @depth=4, @breakables=[], @break=false>
Returns the names of the binding’s local variables as symbols.
def foo a = 1 2.times do |n| binding.local_variables #=> [:a, :n] end end
This method is the short version of the following code:
binding.eval("local_variables")
Make obj
shareable between ractors.
obj
and all the objects it refers to will be frozen, unless they are already shareable.
If copy
keyword is true
, it will copy objects before freezing them, and will not modify obj
or its internal objects.
Note that the specification and implementation of this method are not mature and may be changed in the future.
obj = ['test'] Ractor.shareable?(obj) #=> false Ractor.make_shareable(obj) #=> ["test"] Ractor.shareable?(obj) #=> true obj.frozen? #=> true obj[0].frozen? #=> true # Copy vs non-copy versions: obj1 = ['test'] obj1s = Ractor.make_shareable(obj1) obj1.frozen? #=> true obj1s.object_id == obj1.object_id #=> true obj2 = ['test'] obj2s = Ractor.make_shareable(obj2, copy: true) obj2.frozen? #=> false obj2s.frozen? #=> true obj2s.object_id == obj2.object_id #=> false obj2s[0].object_id == obj2[0].object_id #=> false
See also the “Shareable and unshareable objects” section in the Ractor
class docs.
Changes asynchronous interrupt timing.
interrupt means asynchronous event and corresponding procedure by Thread#raise
, Thread#kill
, signal trap (not supported yet) and main thread termination (if main thread terminates, then all other thread will be killed).
The given hash
has pairs like ExceptionClass => :TimingSymbol
. Where the ExceptionClass is the interrupt handled by the given block. The TimingSymbol can be one of the following symbols:
:immediate
Invoke interrupts immediately.
:on_blocking
Invoke interrupts while BlockingOperation.
:never
Never invoke all interrupts.
BlockingOperation means that the operation will block the calling thread, such as read and write. On CRuby implementation, BlockingOperation is any operation executed without GVL.
Masked asynchronous interrupts are delayed until they are enabled. This method is similar to sigprocmask(3).
Asynchronous interrupts are difficult to use.
If you need to communicate between threads, please consider to use another way such as Queue
.
Or use them with deep understanding about this method.
In this example, we can guard from Thread#raise
exceptions.
Using the :never
TimingSymbol the RuntimeError
exception will always be ignored in the first block of the main thread. In the second ::handle_interrupt
block we can purposefully handle RuntimeError
exceptions.
th = Thread.new do Thread.handle_interrupt(RuntimeError => :never) { begin # You can write resource allocation code safely. Thread.handle_interrupt(RuntimeError => :immediate) { # ... } ensure # You can write resource deallocation code safely. end } end Thread.pass # ... th.raise "stop"
While we are ignoring the RuntimeError
exception, it’s safe to write our resource allocation code. Then, the ensure block is where we can safely deallocate your resources.
It’s possible to stack multiple levels of ::handle_interrupt
blocks in order to control more than one ExceptionClass and TimingSymbol at a time.
Thread.handle_interrupt(FooError => :never) { Thread.handle_interrupt(BarError => :never) { # FooError and BarError are prohibited. } }
All exceptions inherited from the ExceptionClass parameter will be considered.
Thread.handle_interrupt(Exception => :never) { # all exceptions inherited from Exception are prohibited. }
For handling all interrupts, use Object
and not Exception
as the ExceptionClass, as kill/terminate interrupts are not handled by Exception
.
Returns whether or not the asynchronous queue is empty.
Since Thread::handle_interrupt
can be used to defer asynchronous events, this method can be used to determine if there are any deferred events.
If you find this method returns true, then you may finish :never
blocks.
For example, the following method processes deferred asynchronous events immediately.
def Thread.kick_interrupt_immediately Thread.handle_interrupt(Object => :immediate) { Thread.pass } end
If error
is given, then check only for error
type deferred events.
th = Thread.new{ Thread.handle_interrupt(RuntimeError => :on_blocking){ while true ... # reach safe point to invoke interrupt if Thread.pending_interrupt? Thread.handle_interrupt(Object => :immediate){} end ... end } } ... th.raise # stop thread
This example can also be written as the following, which you should use to avoid asynchronous interrupts.
flag = true th = Thread.new{ Thread.handle_interrupt(RuntimeError => :on_blocking){ while true ... # reach safe point to invoke interrupt break if flag == false ... end } } ... flag = false # stop thread
Returns whether or not the asynchronous queue is empty for the target thread.
If error
is given, then check only for error
type deferred events.
See ::pending_interrupt?
for more information.
Returns an array of the names of the thread-local variables (as Symbols).
thr = Thread.new do Thread.current.thread_variable_set(:cat, 'meow') Thread.current.thread_variable_set("dog", 'woof') end thr.join #=> #<Thread:0x401b3f10 dead> thr.thread_variables #=> [:dog, :cat]
Note that these are not fiber local variables. Please see Thread#[]
and Thread#thread_variable_get
for more details.
Returns true
if the given string (or symbol) exists as a thread-local variable.
me = Thread.current me.thread_variable_set(:oliver, "a") me.thread_variable?(:oliver) #=> true me.thread_variable?(:stanley) #=> false
Note that these are not fiber local variables. Please see Thread#[]
and Thread#thread_variable_get
for more details.
Returns the execution stack for the target thread—an array containing backtrace location objects.
See Thread::Backtrace::Location
for more information.
This method behaves similarly to Kernel#caller_locations
except it applies to a specific thread.