Exchange real and effective group IDs and return the new effective group ID. Not available on all platforms.
[Process.gid, Process.egid] #=> [0, 33] Process::GID.re_exchange #=> 0 [Process.gid, Process.egid] #=> [33, 0]
Returns true
if the real and effective group IDs of a process may be exchanged on the current platform.
Check if --yjit-stats
is used.
Discard statistics collected for --yjit-stats
.
Return a hash for statistics generated for the --yjit-stats
command line option. Return nil
when option is not passed or unavailable.
Format and print out counters as a String
. This returns a non-empty content only when --yjit-stats
is enabled.
Calls the given block with each successive grapheme cluster from self
(see Unicode Grapheme Cluster Boundaries); returns self
:
s = "\u0061\u0308-pqr-\u0062\u0308-xyz-\u0063\u0308" # => "ä-pqr-b̈-xyz-c̈" s.each_grapheme_cluster {|gc| print gc, ' ' }
Output:
ä - p q r - b̈ - x y z - c̈
Returns an enumerator if no block is given.
Same as Enumerator#with_index(0)
, i.e. there is no starting offset.
If no block is given, a new Enumerator
is returned that includes the index.
Iterates the given block for each element with an arbitrary object, obj
, and returns obj
If no block is given, returns a new Enumerator
.
to_three = Enumerator.new do |y| 3.times do |x| y << x end end to_three_with_string = to_three.with_object("foo") to_three_with_string.each do |x,string| puts "#{string}: #{x}" end # => foo: 0 # => foo: 1 # => foo: 2
Returns a list of the private instance methods defined in mod. If the optional parameter is false
, the methods of any ancestors are not included.
module Mod def method1() end private :method1 def method2() end end Mod.instance_methods #=> [:method2] Mod.private_instance_methods #=> [:method1]
Returns the Ruby source filename and line number containing the definition of the constant specified. If the named constant is not found, nil
is returned. If the constant is found, but its source location can not be extracted (constant is defined in C code), empty array is returned.
inherit specifies whether to lookup in mod.ancestors
(true
by default).
# test.rb: class A # line 1 C1 = 1 C2 = 2 end module M # line 6 C3 = 3 end class B < A # line 10 include M C4 = 4 end class A # continuation of A definition C2 = 8 # constant redefinition; warned yet allowed end p B.const_source_location('C4') # => ["test.rb", 12] p B.const_source_location('C3') # => ["test.rb", 7] p B.const_source_location('C1') # => ["test.rb", 2] p B.const_source_location('C3', false) # => nil -- don't lookup in ancestors p A.const_source_location('C2') # => ["test.rb", 16] -- actual (last) definition place p Object.const_source_location('B') # => ["test.rb", 10] -- top-level constant could be looked through Object p Object.const_source_location('A') # => ["test.rb", 1] -- class reopening is NOT considered new definition p B.const_source_location('A') # => ["test.rb", 1] -- because Object is in ancestors p M.const_source_location('A') # => ["test.rb", 1] -- Object is not ancestor, but additionally checked for modules p Object.const_source_location('A::C1') # => ["test.rb", 2] -- nesting is supported p Object.const_source_location('String') # => [] -- constant is defined in C code
Returns true
if the named private method is defined by mod. If inherit is set, the lookup will also search mod’s ancestors. String
arguments are converted to symbols.
module A def method1() end end class B private def method2() end end class C < B include A def method3() end end A.method_defined? :method1 #=> true C.private_method_defined? "method1" #=> false C.private_method_defined? "method2" #=> true C.private_method_defined? "method2", true #=> true C.private_method_defined? "method2", false #=> false C.method_defined? "method2" #=> false
Makes existing class methods private. Often used to hide the default constructor new
.
String
arguments are converted to symbols. An Array
of Symbols and/or Strings is also accepted.
class SimpleSingleton # Not thread safe private_class_method :new def SimpleSingleton.create(*args, &block) @me = new(*args, &block) if ! @me @me end end
creates TCP/IP server sockets for host and port. host is optional.
If no block given, it returns an array of listening sockets.
If a block is given, the block is called with the sockets. The value of the block is returned. The socket is closed when this method returns.
If port is 0, actual port number is chosen dynamically. However all sockets in the result has same port number.
# tcp_server_sockets returns two sockets. sockets = Socket.tcp_server_sockets(1296) p sockets #=> [#<Socket:fd 3>, #<Socket:fd 4>] # The sockets contains IPv6 and IPv4 sockets. sockets.each {|s| p s.local_address } #=> #<Addrinfo: [::]:1296 TCP> # #<Addrinfo: 0.0.0.0:1296 TCP> # IPv6 and IPv4 socket has same port number, 53114, even if it is chosen dynamically. sockets = Socket.tcp_server_sockets(0) sockets.each {|s| p s.local_address } #=> #<Addrinfo: [::]:53114 TCP> # #<Addrinfo: 0.0.0.0:53114 TCP> # The block is called with the sockets. Socket.tcp_server_sockets(0) {|sockets| p sockets #=> [#<Socket:fd 3>, #<Socket:fd 4>] }
creates a TCP/IP server on port and calls the block for each connection accepted. The block is called with a socket and a client_address as an Addrinfo
object.
If host is specified, it is used with port to determine the server addresses.
The socket is not closed when the block returns. So application should close it explicitly.
This method calls the block sequentially. It means that the next connection is not accepted until the block returns. So concurrent mechanism, thread for example, should be used to service multiple clients at a time.
Note that Addrinfo.getaddrinfo
is used to determine the server socket addresses. When Addrinfo.getaddrinfo
returns two or more addresses, IPv4 and IPv6 address for example, all of them are used. Socket.tcp_server_loop
succeeds if one socket can be used at least.
# Sequential echo server. # It services only one client at a time. Socket.tcp_server_loop(16807) {|sock, client_addrinfo| begin IO.copy_stream(sock, sock) ensure sock.close end } # Threaded echo server # It services multiple clients at a time. # Note that it may accept connections too much. Socket.tcp_server_loop(16807) {|sock, client_addrinfo| Thread.new { begin IO.copy_stream(sock, sock) ensure sock.close end } }
Return the native thread ID which is used by the Ruby thread.
The ID depends on the OS. (not POSIX thread ID returned by pthread_self(3))
On Linux it is TID returned by gettid(2).
On macOS it is the system-wide unique integral ID of thread returned by pthread_threadid_np(3).
On FreeBSD it is the unique integral ID of the thread returned by pthread_getthreadid_np(3).
On Windows it is the thread identifier returned by GetThreadId().
On other platforms, it raises NotImplementedError
.
NOTE: If the thread is not associated yet or already deassociated with a native thread, it returns nil. If the Ruby implementation uses M:N thread model, the ID may change depending on the timing.
With a block given, calls the block with each element and its index; returns self
:
h = {} (1..4).each_with_index {|element, i| h[element] = i } # => 1..4 h # => {1=>0, 2=>1, 3=>2, 4=>3} h = {} %w[a b c d].each_with_index {|element, i| h[element] = i } # => ["a", "b", "c", "d"] h # => {"a"=>0, "b"=>1, "c"=>2, "d"=>3} a = [] h = {foo: 0, bar: 1, baz: 2} h.each_with_index {|element, i| a.push([i, element]) } # => {:foo=>0, :bar=>1, :baz=>2} a # => [[0, [:foo, 0]], [1, [:bar, 1]], [2, [:baz, 2]]]
With no block given, returns an Enumerator
.
Calls the block once for each element, passing both the element and the given object:
(1..4).each_with_object([]) {|i, a| a.push(i**2) } # => [1, 4, 9, 16] {foo: 0, bar: 1, baz: 2}.each_with_object({}) {|(k, v), h| h[v] = k } # => {0=>:foo, 1=>:bar, 2=>:baz}
With no block given, returns an Enumerator
.
Starts tracing object allocations from the ObjectSpace
extension module.
For example:
require 'objspace' class C include ObjectSpace def foo trace_object_allocations do obj = Object.new p "#{allocation_sourcefile(obj)}:#{allocation_sourceline(obj)}" end end end C.new.foo #=> "objtrace.rb:8"
This example has included the ObjectSpace
module to make it easier to read, but you can also use the ::trace_object_allocations
notation (recommended).
Note that this feature introduces a huge performance decrease and huge memory consumption.