Returns the Ruby source filename and line number of the binding object.
mtch.values_at(index, ...) -> array
Uses each index to access the matching values, returning an array of the corresponding matches.
m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie") m.to_a #=> ["HX1138", "H", "X", "113", "8"] m.values_at(0, 2, -2) #=> ["HX1138", "X", "113"] m.values_at(1..2, -1) #=> ["H", "X", "8"] m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2") m.to_a #=> ["1 + 2", "1", "+", "2"] m.values_at(:a, :b, :op) #=> ["1", "2", "+"]
Load the given PStore
file. If read_only
is true, the unmarshalled Hash
will be returned. If read_only
is false, a 3-tuple will be returned: the unmarshalled Hash
, a checksum of the data, and the size of the data.
Ensures that names
only includes names for the :rdoc, :clobber_rdoc and :rerdoc. If other names are given an ArgumentError
is raised.
Iterates over all IP addresses for name
.
Iterates over all hostnames for address
.
Iterates over all IP addresses for name
.
Iterates over all hostnames for address
.
Returns the Ruby source filename and line number containing this proc or nil
if this proc was not defined in Ruby (i.e. native).
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native).
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native).
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
, the method will copy objects before freezing them This is safer option but it can take be slower.
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.
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.
Converts block to a Proc
object (and therefore binds it at the point of call) and registers it for execution when the program exits. If multiple handlers are registered, they are executed in reverse order of registration.
def do_at_exit(str1) at_exit { print str1 } end at_exit { puts "cruel world" } do_at_exit("goodbye ") exit
produces:
goodbye cruel world
Ruby tries to load the library named string relative to the requiring file’s path. If the file’s path cannot be determined a LoadError
is raised. If a file is loaded true
is returned and false otherwise.
Returns the current execution stack—an array containing backtrace location objects.
See Thread::Backtrace::Location
for more information.
The optional start parameter determines the number of initial stack entries to omit from the top of the stack.
A second optional length
parameter can be used to limit how many entries are returned from the stack.
Returns nil
if start is greater than the size of current execution stack.
Optionally you can pass a range, which will return an array containing the entries within the specified range.
Returns an array containing truthy elements returned by the block.
With a block given, calls the block with successive elements; returns an array containing each truthy value returned by the block:
(0..9).filter_map {|i| i * 2 if i.even? } # => [0, 4, 8, 12, 16] {foo: 0, bar: 1, baz: 2}.filter_map {|key, value| key if value.even? } # => [:foo, :baz]
When no block given, returns an Enumerator.
Returns an array of flattened objects returned by the block.
With a block given, calls the block with successive elements; returns a flattened array of objects returned by the block:
[0, 1, 2, 3].flat_map {|element| -element } # => [0, -1, -2, -3] [0, 1, 2, 3].flat_map {|element| [element, -element] } # => [0, 0, 1, -1, 2, -2, 3, -3] [[0, 1], [2, 3]].flat_map {|e| e + [100] } # => [0, 1, 100, 2, 3, 100] {foo: 0, bar: 1, baz: 2}.flat_map {|key, value| [key, value] } # => [:foo, 0, :bar, 1, :baz, 2]
With no block given, returns an Enumerator.
Alias: collect_concat
.
Returns the elements for which the block returns the maximum values.
With a block given and no argument, returns the element for which the block returns the maximum value:
(1..4).max_by {|element| -element } # => 1 %w[a b c d].max_by {|element| -element.ord } # => "a" {foo: 0, bar: 1, baz: 2}.max_by {|key, value| -value } # => [:foo, 0] [].max_by {|element| -element } # => nil
With a block given and positive integer argument n
given, returns an array containing the n
elements for which the block returns maximum values:
(1..4).max_by(2) {|element| -element } # => [1, 2] %w[a b c d].max_by(2) {|element| -element.ord } # => ["a", "b"] {foo: 0, bar: 1, baz: 2}.max_by(2) {|key, value| -value } # => [[:foo, 0], [:bar, 1]] [].max_by(2) {|element| -element } # => []
Returns an Enumerator
if no block is given.
With a block given, calls the block with each element, but in reverse order; returns self
:
a = [] (1..4).reverse_each {|element| a.push(-element) } # => 1..4 a # => [-4, -3, -2, -1] a = [] %w[a b c d].reverse_each {|element| a.push(element) } # => ["a", "b", "c", "d"] a # => ["d", "c", "b", "a"] a = [] h.reverse_each {|element| a.push(element) } # => {:foo=>0, :bar=>1, :baz=>2} a # => [[:baz, 2], [:bar, 1], [:foo, 0]]
With no block given, returns an Enumerator
.
Calls the given block with each element, converting multiple values from yield to an array; returns self
:
a = [] (1..4).each_entry {|element| a.push(element) } # => 1..4 a # => [1, 2, 3, 4] a = [] h = {foo: 0, bar: 1, baz:2} h.each_entry {|element| a.push(element) } # => {:foo=>0, :bar=>1, :baz=>2} a # => [[:foo, 0], [:bar, 1], [:baz, 2]] class Foo include Enumerable def each yield 1 yield 1, 2 yield end end Foo.new.each_entry {|yielded| p yielded }
Output:
1 [1, 2] nil
With no block given, returns an Enumerator
.
Calls the block with each successive disjoint n
-tuple of elements; returns self
:
a = [] (1..10).each_slice(3) {|tuple| a.push(tuple) } a # => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] a = [] h = {foo: 0, bar: 1, baz: 2, bat: 3, bam: 4} h.each_slice(2) {|tuple| a.push(tuple) } a # => [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]], [[:bam, 4]]]
With no block given, returns an Enumerator
.