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 } }
Returns the limit for field size; used for parsing; see {Option max_field_size
}:
CSV.new('').max_field_size # => nil
Since 3.2.3.
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.
Returns the method identifier for the given object
.
class A include ObjectSpace def foo trace_object_allocations do obj = Object.new p "#{allocation_class_path(obj)}##{allocation_method_id(obj)}" end end end A.new.foo #=> "Class#new"
See ::trace_object_allocations
for more information and examples.
Counts objects for each T_DATA
type.
This method is only for MRI developers interested in performance and memory usage of Ruby programs.
It returns a hash as:
{RubyVM::InstructionSequence=>504, :parser=>5, :barrier=>6, :mutex=>6, Proc=>60, RubyVM::Env=>57, Mutex=>1, Encoding=>99, ThreadGroup=>1, Binding=>1, Thread=>1, RubyVM=>1, :iseq=>1, Random=>1, ARGF.class=>1, Data=>1, :autoload=>3, Time=>2} # T_DATA objects existing at startup on r32276.
If the optional argument, result_hash, is given, it is overwritten and returned. This is intended to avoid probe effect.
The contents of the returned hash is implementation specific and may change in the future.
In this version, keys are Class
object or Symbol
object.
If object is kind of normal (accessible) object, the key is Class
object. If object is not a kind of normal (internal) object, the key is symbol name, registered by rb_data_type_struct.
This method is only expected to work with C Ruby.
Return all reachable objects from ‘obj’.
This method returns all reachable objects from ‘obj’.
If ‘obj’ has two or more references to the same object ‘x’, then returned array only includes one ‘x’ object.
If ‘obj’ is a non-markable (non-heap management) object such as true, false, nil, symbols and Fixnums (and Flonum) then it simply returns nil.
If ‘obj’ has references to an internal object, then it returns instances of ObjectSpace::InternalObjectWrapper
class. This object contains a reference to an internal object and you can check the type of internal object with ‘type’ method.
If ‘obj’ is instance of ObjectSpace::InternalObjectWrapper
class, then this method returns all reachable object from an internal object, which is pointed by ‘obj’.
With this method, you can find memory leaks.
This method is only expected to work except with C Ruby.
Example:
ObjectSpace.reachable_objects_from(['a', 'b', 'c']) #=> [Array, 'a', 'b', 'c'] ObjectSpace.reachable_objects_from(['a', 'a', 'a']) #=> [Array, 'a', 'a', 'a'] # all 'a' strings have different object id ObjectSpace.reachable_objects_from([v = 'a', v, v]) #=> [Array, 'a'] ObjectSpace.reachable_objects_from(1) #=> nil # 1 is not markable (heap managed) object
Returns information about object moved in the most recent GC compaction.
The returned hash has two keys :considered and :moved. The hash for :considered lists the number of objects that were considered for movement by the compactor, and the :moved hash lists the number of objects that were actually moved. Some objects can’t be moved (maybe they were pinned) so these numbers can be used to calculate compaction efficiency.
Returns information about the most recent garbage collection.
If the optional argument, hash, is given, it is overwritten and returned. This is intended to avoid probe effect.
Returns a list of paths matching glob
from the latest gems that can be used by a gem to pick up features from other gems. For example:
Gem.find_latest_files('rdoc/discover').each do |path| load path end
if check_load_path
is true (the default), then find_latest_files
also searches $LOAD_PATH for files as well as gems.
Unlike find_files
, find_latest_files
will return only files from the latest version of a gem.
The file name and line number of the caller of the caller of this method.
depth
is how many layers up the call stack it should go.
e.g.,
def a; Gem.location_of_caller
; end a #=> [“x.rb”, 2] # (it’ll vary depending on file name and line number)
def b; c; end def c; Gem.location_of_caller(2)
; end b #=> [“x.rb”, 6] # (it’ll vary depending on file name and line number)
Returns the latest release-version specification for the gem name
.
Returns the latest release version of RubyGems.
Returns the version of the latest release-version of gem name
Glob pattern for require-able plugin suffixes.
Path to specification files of default gems.
The default signing key path
The default signing certificate chain path